Game Development Reference
To create a believable illusion of motion, these slightly different images need to be flashed in front of
a viewer's eyes at least 12 times per second. (This is what was discussed earlier as fps.) Most cartoon
animation is animated at 12 fps. For really fluid natural motion, you need to increase the frame rate
to about 24 fps. The 24 fps rate is the frame rate used by films shown in a cinema and high-quality
animated films. Video uses a frame rate of roughly 30 fps.
In this project, the fps is set to 30. This means that all the objects on the stage are updated 30 times
per second. Each time Flash does one of these updates, it “enters a frame.” So the program enters a
frame 30 times per second.
In a nutshell, that is what Arajp*AJPAN[BN=IA means. Every time the program enters a new frame, the
AJPAN[BN=IA event is triggered. So whatever directives you put inside an event handler called by an
AJPAN[BN=IA event runs 30 times per second. It runs for the entire duration of the program or until
you remove the event listener.
In the current program, these two directives are being run 30 times per second:
It makes the object appear as though it's moving. Here's how:
Let's imagine that the lh]uan object is at an t position of 100. If the player of the game presses the
left arrow key, the rt variable is assigned the value )1. The next time the SWF “enters a new frame,”
-5 is added to the lh]uan object's t current position. The object's new position is now 95. On the next
frame, -5 is added to the lh]uan object's t position again, so its new position becomes 90. If the left
arrow key is released, the rt variable is assigned a value of ,. Zero is then added to the lh]uan object's
t position (using the '9 operator), so its position remains 90 and it stops moving.
These may seem like a lot of hoops to jump through just to get the lh]uan object to move on the
screen. Hang on for a bit; the advantages of this approach will be very apparent a bit later in the topic
when you look at natural motion using physics simulations. If you can calculate the velocity of the
lh]uan first, there are all kinds of fun things you can do with it before you use it to update the posi-
tion of the lh]uan. Patience, my child; all shall be revealed!
The AJPAN[BN=IA event is one of the most important of AS3.0's events for game designers. It's the
basis for moving objects with programming code in Flash. Most of the new techniques you'll be look-
ing at will be triggered by the AJPAN[BN=IA event, so you'll find that the kjAjpanBn]ia event handler
will become quite a busy, bustling little place from now on—soon to be full of new friends and cheer-
Setting screen boundaries
Now that you can move the little player character around the stage, notice that you can drive it com-
pletely off the edge and keep going on forever and ever if you want to. There are three main strate-
gies that game designers use to prevent this from happening: