Game Development Reference
In-Depth Information
protected function startGame():void {
dispatchEvent(new Event(GAME_START));
protected function gameOver():void {
dispatchEvent(new Event(GAME_OVER));
names are not only public constants but they
re static, which
makes them easily accessible from anywhere. Next, the Sprite class
extends EventDispatcher, so all my methods are ready for me to
use without defining anything extra, which is very convenient.
Finally, I often create and dispatch my events in a single line.
ll rarely need to keep a reference to an event object after you
create it, unless you
re adding a bunch of extra data to it, so I
prefer this method for basic events because it
s one less line to
type and one less variable to assign. Now, let
s look at how another
class might listen to these events.
addEventListener, removeEventListener,
and Event Phases
There are a couple of different ways to listen for events, and these
depend on the phase an event is in. When a DisplayObject on the
stage dispatches an event, it goes through three phases: capture,
target,andbubble.Inthe capture phase, the event travels all the
way from the Stage down through the display list chain to the
DisplayObject that sent the event. Once the event reaches its
it enters what is known as the target phase. Finally, the
event travels back up the display list to the Stage called the
bubbling phase. Any object along the path of the display list can lis-
ten for these types of events, assuming the event is created that way.
The reason for this particular sequence is that objects in the display
list can easily listen for events further down the chain, such as
mouse or keyboard input. For non-DisplayObjects (or Display-
Objects that are not on the stage), events have only one phase, the
target phase. In other words, the only way to listen for these events
is to listen to the object directly. Let
s look at some examples to
make this a little clearer.
ll start with a generic object first since their events are sim-
pler and can only be listened to in one way. This is also the most
common way to listen for messages, during the target phase. To
Search Nedrilad ::

Custom Search