Movement on one axis - source
movie - To restart, right-click and
select "restart"
propertysp
onbeginsprite(me)
sp = sprite(me.spritenum) end
onexitframe()
sp.loch = sp.loch
+ 1 end
A script shown with a movie is the behavior script attached to the sprite.
If you copy the script from the web page to Director, the invisible characters
from the page may cause script errors. To fix, either delete them or get
the script from the source movie.
This is what is happening in the exitframe
handler. The computer first evaluates the right side
of the "=" statement and calculates 1 plus the current value
of sp.loch.
The result of this calculation is then put in
the variable on the left side of the "=" statement,
which in this case is also sp.loch.
The handler is then finished.
The value of sp.loch
is now one greater than before the handler was executed. Each time the
handler executes, sp.loch
will increase by one. On the screen, the sprite moves to the right.
Since the sprite is moving one pixel each frame, you could
say that the sprite has a velocity of 1 px/frame
to the right.
•How would you move the sprite 2 px/frame
to the right? How would you move the sprite to the left?
Velocity Using a variable for the velocity makes obvious what
is happening:
propertysp propertyvelocity
onbeginspriteme
sp = sprite(me.spritenum) velocity = 1 end
onexitframeme
sp.loch = sp.loch
+ velocity end
This script does the same thing as the first, but
it makes the expression easier to understand. Also, the
velocity value can be used or changed in other places
in the code. The new variable velocity
was declared as a property
so that it's value is not forgotten between handlers.
In the beginspritehandler, velocity
is being set to an initial value of 1, or initialized to 1. What
would happen if velocity was set to a larger value? As you probably
guessed, the sprite will move to the right
faster. If it is set to a negative number, the sprite will move
to the left. (Try it!)