Here is a basic technique - quite simple really. I've added comments to the code to make it easier to follow.
sync on
sync rate 60
` ** Create a demo sprite, with backdraw disabled
box 0, 0, 16, 16
get image 1, 0, 0, 16, 16
sprite 1, 320, 240, 1
set sprite 1, 0, 1
offset sprite 1, sprite width(1)/2, sprite height(1)/2
` ** A few variable definitions
` The number of moves still to make to hit the target
MovesLeft as integer
` The target
TargetX as float
TargetY as float
` The current position
CurrentX as float
CurrentY as float
` The size of the steps to take
StepX as float
StepY as float
` A timer to stop the program reacting to the mouse click for a while
ClickTimeout as integer
repeat
cls
` If the timer has run out
if ClickTimeout < timer()
` If a mouse button has been clicked
if mouseclick() > 0
` Snapshot the new target position and the current sprite position
TargetX = mousex()
TargetY = mousey()
CurrentX = sprite x(1)
CurrentY = sprite y(1)
` Calculate the number of moves to take - I'm just using the distance in pixels
MovesLeft = sqrt( (TargetX - CurrentX)^2 + (TargetY - CurrentY)^2 )
` Calculate the size of the steps to take in the x and y directions
StepX = (TargetX - CurrentX) / MovesLeft
StepY = (TargetY - CurrentY) / MovesLeft
` Set the timer to *not* react to a mouseclick within the next half-second
ClickTimeout = timer() + 500
endif
endif
` If we still have moves to take
if MovesLeft > 0
` If this is the last step
if MovesLeft = 1
` Place the sprite on the final target
sprite 1, TargetX, TargetY, 1
else
` Update the current position with the step sizes
inc CurrentX, StepX
inc CurrentY, StepY
` Position the sprite at that position
sprite 1, CurrentX, CurrentY, 1
endif
` Decrease the number of moves left
dec MovesLeft
endif
` Update the display
sync
until spacekey() > 0
*** Coming soon - Network Plug-in - Check my site for info ***
For free Plug-ins, source and the Interface library for Visual C++ 6, .NET and now for Dev-C++
http://www.matrix1.demon.co.uk