Move Object between two points

Started by kevin, November 23, 2009, 11:57:43 AM

Previous topic - Next topic

kevin

 Move Object between two points

This example lets the user move a circle around the screen by clicking it's destination position.  So when you click the object will move between it's current position and the target over the period of one second.



PlayBASIC Code: [Select]
; objects current position & size
Xpos#=400
Ypos#=300
Radius=20

; selection modes =0 Target Not selected, 1 = moving to target.
Mode = 0



setfps 60

Do
cls 0


if Mode=0
if MouseButton()=1
Mode=1

StartingX#=Xpos#
StartingY#=Ypos#
StartTime =Timer()
TargetX#=mousex()
TargetY#=mousey()

EndTime =StartTime+1000


endif
endif



if Mode=1
TimePast#=Timer()-StartTime
Scaler#=TimePast#/(endTime-StartTime)
if Scaler#>1
Scaler#=1
Mode=0
endif

dx#=(TargetX#-StartingX#)*Scaler#
dy#=(TargetY#-StartingY#)*Scaler#

xpos#=StartingX#+Dx#
ypos#=StartingY#+Dy#



circle StartingX#,StartingY#,Radius,false
circle TargetX#,Targety#,Radius,false




endif


circle Xpos#,Ypos#,Radius,true


Sync
loop