Categories Displayed in Flash

Flash: Working with Events: Get on the bus..Bubbling is your friend.

One of the great changes in AS3 is the DisplayList.   It when used appropriately, dramatically eases many things, one of my favorite is decoupling clip intercommunication, as the display list or any clip on it can act as a message bus.

Approach 1 : Display list heirarchy as message bus.

Say you have a monkey button shaped as a deep in a forest of tree graphics that you want to find out when it's been clicked on, even while it's jumping around from branch to branch (at different depths).   If a event setup on the click is set to bubble it doesn't matter where or how deep it is, it will eventually bubble up to the parent class so you can do something with it.

Similarly say you have a animated figure that you want to fire off when it's completed with it's animation, to do something else. While one possibility is the 'controller' reaching down to watch (e.g. using ENTER_FRAME, or addFrameScript(totalFrames-1..), or the clip reaching up and telling the main timeline to do something.  A much cleaner approach is listening for the event closer to the root (e.g. a document level class), and  dispatching an event on the last frame like:

dispatchEvent( new Event("FINISHED_ANIMATION", true, true)); //last help it bubble

 Approach 2: Stage as message bus.

Third is using a stage as a message bus, since every clip has access to it.   Especially when used with the ADDED_TO_STAGE event and dynamically loaded assets that have to perform some functions before being ready or passing a set of values to a caring class, when using an event that can carry a generic message, like EventWithArgs.

1 responses to 'Flash: Working with Events: Get on the bus..Bubbling is your friend.'

Made on 24.7.08 @ 7:43 pm

[...] For more info see here. [...]

Add a Comment: