News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

problem with simple program

Started by Kman1011, November 12, 2006, 08:42:42 PM

Previous topic - Next topic

Kman1011

I started trying to program a sprite to attach to the mouse cursor. For some reason there is a delay when the mouse cursor is moved. The sprite takes a second  to 'catch up' the the cursor.

There is a program just like it called setting sprite images in the examples. I followed the code almost exactly. (Slight variation). But the basic principal is the same. That program, the sprite sits on the cusor with do delay in movement

Here is my code
PlayBASIC Code: [Select]
; PROJECT : Project-1

;First Two lines are for loaded image

;LoadImage CurrentDir$()+"GameGFX\Gun.bmp",1;find and load image to put in sprite
;ImageMaskColour 1,rgb(255,255,255);take out white background
boxC 0,0,64,64,1,RGB(37, 197, 137)
MyImage=GetFreeImage()
getImage MyImage,0,0,64,64
createsprite 1;create a sprite container
SpriteImage 1,MyImage;fill the container with loaded image
autocenterspritehandle 1,true

Do
cls 0 ;clear the screen
mx#=MouseX();aquire mouse X & Y positions into variables
my#=MouseY()

PositionSprite 1,mx#,my#;place sprite at mouse cursor point
DrawSprite 1;place sprite on the screen
Sync;show screen
loop

 

I running on a 3 GHz Pentium. None of the other examples are slow. Hoping I could get some insight to what I'm doing wrong
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA

Ian Price

Your program is actually running slower than the screen refresh rate of your monitor, hence the slowdown.

You could add "SetFPS 60" at the start of the program to rectify this.

You could also hide the Windows Mouse pointer (Turn it on/off with "Mouse ???") and replace it with a PB image, this would keep the mouse and the object at the same speed.

Does that help?
I came. I saw. I played some Nintendo.

kevin

#2

   Yes, this demonstrates an oddity.  Where PB is getting swamped by mouse events.    There's a few ways to address this.  The first is what Ian suggested.  But it you don't want to limit the frame rate (sync rate), then another quirky  method, is the use the point command and read from the screen  (put Col=point(0,0) at the start of you loop), or draw points/lines/boxes, text etc.  Odd, I know !


Quote
boxC 0,0,64,64,1,RGB(37, 197, 137)
  MyImage=GetFreeImage()
getImage MyImage,0,0,64,64
createsprite 1;create a sprite container
SpriteImage 1,MyImage;fill the container with loaded image
autocenterspritehandle 1,true


Do

   cls 0   ;clear the screen

Col=point(0,0)   ;

   mx#=MouseX();aquire mouse X & Y positions into variables
   my#=MouseY()
   
   PositionSprite 1,mx#,my#;place sprite at mouse cursor point
   DrawSprite 1;place sprite on the screen
Sync;show screen
loop


Kman1011

#3
Bravo guys!!! :D

Both concepts worked!!!. I have heard of the SetFPS command, but not sure what it did or where it should be set. Its certainly is a oddity. Kevins method worked as well although I'm not sure why.

But here's something wierd. I fiddled with the example 'Setting Sprite images' and I found I had the same problem when I took out the function 'Show_Sprite_info' in the program. It would seem that just by using the text showing, helped somehow.
So I just printed a blank like so in the actual function.

PlayBASIC Code: [Select]
; PROJECT : SpriteExample
; AUTHOR : Underware Design
; CREATED : 8/13/2004
; EDITED : 11/12/2006





;============================
; First Make an Shaded Box Image
;============================

Col=RGB(25,176,255)

size=128

ColScaler#=100.0/(size/2)
For lp=0 To size/2
BoxC lp,lp,size-lp,size-lp,1,RGBFade(Col,50+(lp*ColScaler#))
Next


MyImage=GetFreeImage()

GetImage MyImage,0,0,size,size




;============================
; CReate a Sprite
;============================

MySprite=GetFreeSprite()
CreateSprite MySprite
SpriteImage MySprite,MyImage

PositionSprite MySprite,200,200


autocenterspritehandle 1,true

Do

Cls 0


x#=MouseX()
y#=MouseY()

PositionSprite MySprite,x#,y#
DrawSprite MySprite

Show_sprite_info(MySprite)


Sync

Loop



Function Show_Sprite_info(ThisSprite)

x#=GetSpriteX(ThisSprite)
y#=GetSpriteY(ThisSprite)

CursorMargin x#
SetCursor x#,y#+GetSpriteHeight(ThisSprite)

Print " "
;Print "Sprite Y:"+Str$(y#)
;Print "Sprite Image:"+Str$(GetSpriteImage(ThisSprite))
;Print "Sprite Width:"+Str$(GetSpriteWidth(ThisSprite))
;Print "Sprite Height:"+Str$(GetSpriteHeight(ThisSprite))


EndFunction




This is what made it work. Well at least I got several solutions

Thanx again :)
Ahh... Another visitor. Stay awhile....STAY FOREVER!!!...MWA-HA-HA-HA