Usually you'll want to have your animated object react
to certain conditions. For example, in the two-axes
demo it would be nice to have the object react to reaching the edge
of the stage by bouncing off. Other conditions might be a user
action, the passing of a certain amount of time,
the state of another object you are animating, a random
event, or many others.
The first step is to express the condition in terms
of the variables in your program. In the two-axes demo, you could
express that the object has reached the bottom edge of the stage by saying
the vertical position of the sprite is greater than the height
of the stage, or:
sp.locv
> (the stage).rect.height
•How would you express the condition that it reached
the top of the stage?
•Right or left of the stage?
•Why is the comparison (>) operator used instead of equality?
(think when velocity is greater than one)
The next step is to express the desired reaction
in terms of the variables in your program. What should the object
do when it reaches the bottom edge of the stage? It should
start going up instead of down. This means that the vertical velocity
should change, which is stored in the yVelocity
variable.
What should it be changed to? If you
wanted you could make it take off like a rocket with
yVelocity = -20,
but since it was going 1 px/frame downward it would make sense if it started
going 1 px/frame upward. This means changing yVelocity
from 1 to -1 (remember locv gets smaller going upward).
The opposite happens when the object reaches the top of the stage:
yVelocity changes from -1 to 1.
Both of these changes can be accomplished by the statement:
yVelocity = -yVelocity
•How would you express the reaction when the object
reaches the right or left edge of the stage?
Finally, the condition and reaction statements are put
into an if-then statement:
if sp.locv
> (thestage).rect.heightor sp.locv
< 0 then
yVelocity = -yVelocity end if
Every time the object is moved, it has the potential
to meet these conditions. Therefore, the conditions should be
checked every time the object moves. The usual way to do this is to place
the if-then in the same block of code where the object is moved.
The full script:
propertysp propertyxVelocity,
yVelocity
onbeginsprite(me)
sp = sprite(me.spritenum)
--initialize velocity x &
y
xVelocity = 5
yVelocity = 1 end
onexitframe() --check conditions
if sp.loch >
(thestage).rect.widthor sp.loch
< 0 then
xVelocity = -xVelocity end if
if sp.locv
> (thestage).rect.heightor sp.locv
< 0 then
yVelocity = -yVelocity end if
--increment position sp.loch
= sp.loch + xVelocity sp.locv
= sp.locv + yVelocity end
•Why does the object move half-way off the stage
before "bouncing"? How would you change the conditional (if-then)
to keep that from happening?
•What would happen if you initialized xVelocity
and yVelocity to random values between, say, 1 and 10? i.e. "xVelocity
= random(10)"
The type of conditions checked for in this script are called
boundary conditions. They keep the value of a variable
within certain bounds. In this case the values of the object's position
are kept within the boundary of the stage. It is very common
to check for boundary conditions.
There are many other ways to make the
object react when it reaches the edge of the stage. For example, you could
merely limit it to the boundary with
if
sp.loch < 0 then
sp.loch = 0
Or you could make it "wrap" around the stage
with
if
sp.loch < 0 then
sp.loch = (thestage).rect.width