smoothly moving sprites


[ Zettels Traum ] [ search / suche ]

von dp am 06.Maerz 97 um 01:40:40:

zu: Perlen aus dem Strom der Nachrichten im direct_L von Daniel am 22.Dezember 96 um 02:00:59:

>>is it possible to make a
>>sprite
>>move in a certain angle say 39 degrees.
>Here's some code to move a sprite from one point to another, kind of a
>Lingo version of doing an In Between:
>
>-- drag sprite sNum from its current location to newLoc
>-- example: dragSprite 48, point (150, 230)
>on dragSprite sNum, newLoc
>
> set x0 to the locH of sprite sNum
> set y0 to the locV of sprite sNum
> set dx to the locH of newLoc - x0
> set dy to the locV of newLoc - y0
>
> set stepCount to integer (sqrt ((dx * dx) + (dy * dy)) / 2)
> repeat with i = 1 to stepCount
> set percent to 1.0 * i / stepCount
> set spotLoc to point (x0 + dx * percent, y0 + dy * percent)
> set the loc of sprite sNum to spotLoc
> updateStage
> end repeat
>end dragSprite


Or a touch more simply:

set pos0 = the loc of sprite sNum
set vector = newLoc - pos0

set dx = the locH of vector
set dy = the locV of vector

set stepCount to integer (sqrt (dx * dx) + (dy * dy)) / 2)
repeat with i = 1 to stepCount
set the loc of sprite sNum = pos0 + vector * (i / stepCount)
updateStage
end repeat

If timing rather than smoothness is your aim:

set pos0 = the loc of sprite sNum
set vector = newLoc - pos0

set period = 60 --amount of time for the movement to happen in ticks

starttimer

repeat while the timer < period
set the loc of sprite sNum = pos0 + vector * (the timer / period)
updateStage
end repeat
set the loc of sprite sNum = pos0 + vector
--usually necessary to move the sprite into the exact location




Dazu:























D. Plänitz