Game Development Reference
In-Depth Information
listen for an object
sevents,yousimplycallits addEventListener
method and pass it a number of parameters. We
'
ll use the example
of the Game class from above, assuming that there is an instance
of this class in the document class of an FLA.
'
package {
import flash.display.Sprite;
import flash.events.Event;
public class Document extends Sprite {
public var game:Game;
public function Document() {
game = new Game();
game.addEventListener(Game.GAME_START,
gameStart);
game.addEventListener(Game.GAME_OVER,
gameOver);
addChild(game);
}
protected function gameStart(e:Event):void {
trace(e);
}
protected function gameOver(e:Event):void {
trace(e);
}
}
}
The two required parameters of the addEventListener method are
an event type (as a string) and a method to call when that event
occurs. Note that I can use protected (or private, or internal) methods
as my event listeners
this is the only time where something that
occurs outside this class can access an otherwise off-limits method. A
method that is set up to receive events must accept a single para-
meter, the event object. There are few other parameters that are
optional when setting up a listener, and I actually like to assign them,
control freak that I am. The third parameter is useCapture ,whichis
false by default. We will cover it momentarily as it deals with event
phases and display list. The fourth parameter is the listener priority,
which is 0 by default. The priority level tells Flash which listeners
should get the event first
the higher the number, the higher the
priority. If it is critical for one object to receive an event before
another, this is the best way to ensure that. I usually leave it at 0.
Finally, the fifth parameter is useWeakReference ,whichmight
be the coolest feature of events, and is false by default. To fully
Search Nedrilad ::




Custom Search