When either the vertical or horizontal position of a sprite
is set in Director, it is automatically rounded to the nearest
integer. If sp.locv
is set to 10.4, its value will be rounded to 10.
The result of this is that the common statement to increment
a sprite's position:
sp.locv
= sp.locv
+ velocity (or loch)
will allow only integral velocities. If velocity
is set to 1.4, the resulting velocity on screen will
actually be 1 px/frame because the .4 gets rounded off
every time. This is most noticeable when accelerating at speeds
close to zero.
Notice how the upper circle jerks from 1 to 2 to 3 px/frame
as it accelerates. The solution is to use horizontal and vertical
position variables which are initialized to the sprite position.
Those are then incremented during the animation, and loch
and locv are set from those. The thing
to avoid is using loch or locv
in the calculation of the next loch or
locv. The idea is shown in simple form
here for loch:
Round-off Effect:
Solution:
propertysp
onbeginsprite(me)
sp = sprite(me.spritenum) end
onexitframe()
sp.loch = sp.loch
+ 1.5 end
propertysp propertyx--horizontal
loc
onbeginsprite(me)
sp = sprite(me.spritenum)
x = sp.loch--initialize end
onexitframe()
x = x + 1.5--increment
x
sp.loch = x --set
property end
The script on the left moves a sprite 4 pixels
in the first 2 frames. The script on the right moves it 3
pixels in the first 2 frames which is the desired velocity of
1.5 px/frame.
This concept applies to any property that Director
automatically rounds off, which includes loch, locv, width,
height, and others.
Parametric animation does not rely on
previous positions to calculate new ones, and so is not affectedsignificantly by round-off.