Game Development Reference
You need to know the stage's height and width to calculate the stage boundaries. But why were those
properties copied into variables instead of being accessed directly? It's related to a problem that will
cause a lot of frustration if you don't understand it properly.
Here's the problem. Only the document class (for example, I]ej, which was used in earlier chapters)
has direct access to the op]ca object. If you try to access the op]ca object in any other class that isn't
directly referenced by the document class, that class can't “see” the stage. If you make a reference
to the op]ca object in one of these classes, you'll get an error message: Cannot access a property or
method of a null object reference.
There are a few ways to get around this:
An object that uses an event listener attached to the op]ca object doesn't have this problem
because the op]ca object is actively involved in that class. Here's an example:
Objects that use code like this have no trouble making references to the stage directly.
An object doesn't know that the stage exists until it's been added to the stage . That's one of the
reasons you've been using the =@@A@[PK[OP=CA event. When the kj=``a`PkOp]ca event han-
dler runs, you can freely access the op]ca object in the class.
Even though you can access the op]ca object directly in the kj=``a`PkOp]ca event handler,
you might not be able to access it anywhere else in the class. This could be very important if
your game relies on op]ca*op]caDaecdp and op]ca*op]caSe`pd to define stage boundaries.
The way around this is to create a property in the kj=``a`PkOp]ca event handler, which makes
a reference to the op]ca object. You can then use that property anywhere in the class, and it
will behave as if you were accessing the op]ca object directly.
It's this third scenario that you're dealing with in the >qhhap[Bkqn@ena_pekjo class. You have to
copy the stage's op]caDaecdp and op]caSe`pd properties into the class's own [op]caDaecdp and
[op]caSe`pd variables so those values can be used elsewhere in the class.
Have a look at the class again and see if you can find the spot where these properties are being used
to define stage boundaries. And remember these solutions because this problem is one of the biggest
pitfalls programmers new to AS3.0 fall into when they begin game design.
Removing objects from the game
Let's take a more detailed look at what happens when objects are removed from the game. Most of
the objects are removed using naikra?deh`, like this:
To completely clear the object from memory, give any variables that refer to the object a jqhh value,