Game Development Reference
In-Depth Information
Events aren't broadcast to every object in the program at the same time, however. They either travel
up or down through the hierarchy of objects. You can listen to events at any point in this journey.
If an event is set to bubble, it means that the event travels up through the hierarchy. It starts with the
child object and then informs the parent object. If other objects want to listen to that event, they have
to do it by attaching their event listeners to the parent object. Often, that object will be op]ca, which
is the common parent of all display objects. Figure 10-23 illustrates a simple example.
OP=CA
ÎdahhkÏ
op]ca*]``ArajpHeopajan$dahhk(kjDahhk%7
op]ca*]``ArajpHeopajan$dahhk(kjDahhk%7
2. Events bubble up to
the parent container.
3. Objects can listen to
events by attaching a
listener to the parent
container.
Robot Class
Cat Class
Fairy Class
op]ca*]``ArajpHeopajan$dahhk(kjDahhk%7
lner]pabqj_pekjkjDahhk$arajp6Arajp%
w
pn]_a$arajp*p]ncap'eodana%7
pn]_a$Pdeo_kqh`^a^]`%7
[eoJanrkqo9pnqa7
[iuAjaieao*lqod$Ikrea?hel$arajp*p]ncap%%7
y
`eol]p_dArajp$jasArajp$dahhk(pnqa%%7
op]ca*]``ArajpHeopajan$dahhk(kjDahhk%7
lner]pabqj_pekjkjDahhk$arajp6Arajp%
w
pn]_a$Dahhk('arajp*p]ncap'%7
pn]_a$Sah_kiapkpdac]ia%7
y
1. An object dispatches an event.
4. Listening objects can create an event handler
to perform actions when the event happens. They
can access the object that called the event by using
the event's target property.
Figure 10-23. Objects can listen to bubbling events from other objects by attaching event listeners to a common
parent object, such as the stage.
The `eol]p_dArajp method has one argument, which is called the event constructor :
`eol]p_dArajp$jasArajp$arajpJ]ia(pnqa%%7
The event constructor creates the event. Events can be made to bubble by setting the second argu-
ment in the event constructor to pnqa:
jasArajp$arajpJ]ia( pnqa %
If you change it to b]hoa or leave it out entirely, the event doesn't bubble.
 
Search Nedrilad ::




Custom Search