Game Development Reference
Synchronizing ENTER_FRAME events
There's one extremely important technical detail you need to be aware of for the player vs. wall col-
lision system to work accurately. In this game, there are many AJPAN[BN=IA events running on many
different objects. AS3.0 doesn't run them all at the same time. It runs them in the order in which the
objects were added to the stage—either by you dragging them from the Library or adding them with
code using ]``?deh`. That means if the lh]uan object were added to the stage before the wall objects,
all its code, including its AJPAN[BN=IA event, would run before the code in the wall objects does.
Why is this important to be aware of? If the wall objects
run their code first and call the ?khheoekj*^hk_g method
before the player's kjAjpanBn]ia event runs, the player's
[rt and [ru values will be those from the previous frame .
Yes, the previous frame! This will throw the accuracy of the
collision detection off by one frame, and the player will
appear to overlap with the walls slightly when it touches
them. Figure 8-26 illustrates how this looks.
Because the lh]uan is a moving object, its AJPAN[BN=IA
event needs to run before the walls' AJPAN[BN=IA events
for the collision detection to be accurate. In Dungeon Maze
Adventure, I made sure that the lh]uan's code runs first by
dragging the lh]uan object onto the stage before adding
any of the wall objects. This is a very primitive but effective
way of making sure it updates its velocity before the walls
Figure 8-26. If the collision-detection
code runs before the object's positions
have been updated, the acccuracy will
be off by one frame.
You will invariably run into this problem in your own games at some point. You'll notice that the col-
lision detection is maddeningly off by one frame and you won't know why. Well, now you do! Adding
objects to the game in the order you want their code to run is one solution.
Another is this: use only one AJPAN[BN=IA event in your game in a manager class such as @qjcakjKja[
I]j]can. Use that event to call methods in other classes that behave like AJPAN[BN=IA events, but
Here's an example. In the manager class, use code that looks like this: