If each node must be responsible for the dispatch to its children, then
yes, you're probably doing it the right way.  It'll chew up some cycles,
but all tree walks effectively do.  The only way to "optimize" is to
flatten the tree at some point which breaks the abstraction you want.

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of thirtyfivemph
Sent: Thursday, February 14, 2008 8:17 AM
To: flexcoders@yahoogroups.com
Subject: [flexcoders] Re: Broadcasting event to all objects on display
list

 

Actually, I'm trying to avoid each DisplayObject tracing its way back
to the root (which, at any given time, it may not be able to do
depending on where it is in the DisplayList, on or off the stage, etc.).

Here's a more succinct way to ask my question:

Given that the display list is a classic tree (one parent, many
children), is there any mechanism that is "natural" and efficient
within Flash's existing event architecture that would allow me to
dispatch an event from a parent DisplayObject and have that event
propagate down the tree through children, grandchildren, etc.?

Currently, I only see this as the most compact solution:

Each DisplayObject listens for the ADDED and REMOVED events. When
these events occur, the DisplayObject adds a listener to its parent
for the desired custom event. In the listener, the DisplayObject
"redispatches" the event so that any of its children who are listening
can receive it, and they then do the same thing. Thus, if I ever call
myDisplayObject.dispatchEvent() all of myDisplayObject's children,
grandchildren, great-grandchildren, etc. will eventually receive the
event, without anyone having to know anything more than their parent
and children.

Basically, it's propagating events like messages through a network
topology: each node figures out how to broadcast to the next node, no
one node has to know about all of the other nodes, just its immediate
neighbors.

If my solution above sounds like the best route, and it sounds
reasonably efficient (my real concern), I'll probably just create a
new base class for all my objects that implements this.

Troy.

 

Reply via email to