Game Development Reference
In-Depth Information
Adding and removing objects from the stage
Before you look at the code of the game, I need to explain the new way in which classes are being ini-
tialized. Dungeon Maze Adventure has been planned as a multilevel game. If you want to add another
level, it means that you have to remove `qjcakjKja from the stage and add `qjcakjPsk. This gets you
into a tricky technical situation regarding how AS3.0 initializes objects in the Flash Player when the
game runs. It's an extremely important issue to be aware of because it will plague the development of
your Flash games and give you countless sleepless nights if you don't understand why it happens and
how to solve it.
Let's take a look a look at this problem. When the published SWF of the game runs, it does two things
(in this order):
1. It initializes all the objects by running the constructor methods in their classes.
2. It displays the objects on the stage. (It displays them one by one in the order in which they
were dragged to the stage from the Library when you designed the game in the FLA.)
Here's the issue: if the class's constructor method includes a reference to the op]ca or an object that
is on the stage that hasn't yet been displayed, you'll get an error message saying this:
Annkn-,,56?]jjkp]__aoo]lnklanpukniapdk`
kb]jqhhk^fa_pnabanaj_a
If you're adding objects to the game, there's a good chance that many of those objects have to access
the stage as soon as they're created. They might need to do this to set stage boundaries or access the
properties of other objects that are also on the stage. If they can't find the stage object because they
haven't been added to the stage yet, AS3.0's compiler will “throw its hands in the air” and you'll see
the preceding error message.
To solve this problem, you need to initialize objects only when they they've been added to the stage,
not before.
ADDED_TO_STAGE event
In earlier chapters, you initialized the classes like this:
lq^he__h]ooI]ejatpaj`oIkrea?hel
w
lq^he_bqj_pekjI]ej$%
w
ejep$%7
y
bqj_pekjejep$%6rke`
w
++Ejepe]heva_h]oo***
y
y
 
Search Nedrilad ::




Custom Search