Game Development Reference
In-Depth Information
3. Finally, you need to restart the animation using the op]np method:
psaajK^fa_p*op]np$%7
There's a bit of a conceptual leap you have to make here because you need to keep in mind that
the entirety of the robot's animation is contained with the Psaaj objects as abstract code. There's
no timeline, Movie Clip, or AJPAN[BN=IA loop that is making the animation happen. The animation is
completely created and runs within the Psaaj object itself.
This is actually very convenient because it takes the responsibility of managing the animation away
from the rest of the code. You can create as many Psaaj objects as you like and then just let them lose
to do their thing. The code that actually makes the animation work is neatly hidden from you in the
inner workings of the Psaaj class.
Calculating velocity
There's one weakness in this system that you might have already noticed. In the previous chapter, all
the physics and collision detection code were dependent on knowing the objects' vertical and hori-
zontal velocities: [rt and [ru. It's easy to calculate velocity in the context on an AJPAN[BN=IA event.
But there are no rt and ru properties you can access on Psaaj objects. This means that if you want
to change something about the game based on how quickly the object is moving or have it react to
a collision with another object, you can't do that directly.
The solution is to create an AJPAN[BN=IA event handler that tracks the object being animated and
calculates its velocity. You can use a very simple system to calculate the object's velocity. Here's a sim-
plified version of what these calculations look like:
rt9t)kh`T7
ru9u)kh`U7
kh`T9t7
kh`U9u7
You'll see how this is used and why it works in a moment.
In the example code, you created all the variables you need to do this directly in the nk^kp object as
dynamic instance variables:
nk^kp*rt9,7
nk^kp*ru9,7
nk^kp*kh`T9,7
nk^kp*kh`U9,7
As discussed in the previous chapter, dynamic instance variables are properties that
can be created on objects as you need them. To be able to do this, the class that the
object extends needs to be declared as `uj]ie_ . The Ikrea?hel class is a dynamic
class, so you can create dynamic instance variables on all Movie Clip objects. It's gen-
erally considered risky programming, but it is undoubtedly convenient in small pro-
grams like this one.
 
Search Nedrilad ::




Custom Search