Game Development Reference
In-Depth Information
appreciate what it does, you first need to understand how garbage
collector of Flash (the mechanism that removes unused objects
from memory) works. We
ll cover the garbage collector more in
depthwhenwereachthesectiononFlashidiosyncrasies,butsuf-
fice it to say for the moment that by setting useWeakReference to
true, the listener will automatically be removed when the object it
is listening to is deleted from memory. Unless you have a specific
reason you do not want the listener to be removed automatically, I
recommend always setting useWeakReference to true. The follow-
ing is a modification of the two lines from the above example,
written to use weak references.
'
game.addEventListener(Game.GAME_START, gameStart, false, 0, true);
game.addEventListener(Game.GAME_OVER, gameOver, false, 0, true);
When you no longer need to listen for an event (or if you
are not using weakly reference listeners), you can use the
removeEventListener method with the same first three parameters
you called in addEventListener to disengage a listener from an
object. For the example above, once the Document class was
done listening to the game for events, it could call the following
two lines:
game.removeEventListener(Game.GAME_START, gameStart, false);
game.removeEventListener(Game.GAME_OVER, gameOver, false);
Like addEventListener, the third parameter is optional, depend-
ing on whether you
'
re using the capture phase, which we will
cover now.
As I mentioned earlier, if you
re passing events between
DisplayObjects, you have a few different options available to you,
depending on where your objects are in relation to each other.
Here are some different situations:
Scenario #1: The object you want to listen to is a child object,
either directly or through the display chain, of your current object.
In this instance, you can listen to the target object in all three
phases. When you listen during the capture or bubbling phases,
you don
'
t add the listener to the object itself, but rather the object
that is listening, as the event will be broadcast to your object as it
'
By default, most events do not bubble unless
explicitly told to do so. To be able to listen to the events of Game
class from all three phases, we would first have to modify the
dispatchEvent calls to look the following:
passes through.
dispatchEvent(new Event(GAME_START, true));
dispatchEvent(new Event(GAME_OVER, true));
The second parameter when creating a new event tells the event
whether or not to bubble. Now that these events are bubbling, we
Search Nedrilad ::




Custom Search