Game Development Reference
What actually happens to the object when it's removed? AS3.0 has a garbage collector to do the job.
You can think of the garbage collector as a little software robot that runs around your game looking
for objects and properties that aren't being used or don't have any value, and deletes them for good.
This saves memory and processing power—thanks, Flash! One of the jobs of the garbage collector is to
find objects that have been removed with naikra?deh`, and wipe them from Flash Player's memory.
However, the garbage collector is a bit of a finicky fellow, and very picky about what it chooses to
The garbage collector doesn't delete objects that have an AJPAN[BN=IA event running. These
objects make the garbage collector a bit squeamish, so you have to manually remove the
AJPAN[BN=IA event with naikraHeopajan, as you did with the naikra`BnkiOp]ca event han-
dlers. Although objects remove their own event listeners (such as =@@A@[PK[OP=CA and
NAIKRA@[BNKI[OP=CA) it's considered best practice to also remove them manually.
The garbage collector doesn't like Movie Clip objects that are animated using a timeline, such
as the two enemy objects. It doesn't touch them unless the animation is stopped by using the
If any objects have timers running using the Peian class (more on timers in Chapter 10), the
garbage collector doesn't delete them.
The garbage collector doesn't delete an object if there's more than one variable in the game that
makes a reference to it. For example, imagine that you have two variables, lh]uanK^fa_pKja and
lh]uanK^fa_pPsk, which both contain a reference to the lh]uan object:
Imagine that in your program you try and remove the lh]uan object like this:
The lh]uan object is removed from the stage, but the garbage collector doesn't delete it from
memory because the lh]uanK^fa_pPsk variable still contains a reference to it.
The only way to coerce the garbage collector to completely delete the lh]uan object is if every
variable that holds a reference to it is assigned a value of jqhh (which is a value used with
Movie Clip objects to wipe them clean of all data). It's not hard to do, and a directive like this
will do the trick:
One more thing: you have to make sure that every reference to the lh]uan object in every class
that might have made one also sets those variables to jqhh. Only then does the garbage col-
lector come back with his broom and finish the job. And if that's the case, the lh]uan object is
then completely deleted.
I went into quite a bit of detail into this issue because if you spend any time working with Flash (which
you will!) you'll inevitably come across issues in your games where quirky things start to happen
because of objects still actually existing, or running listeners or animations in the background, when
you think they're gone.