Peter Elst

Flash Platform Consultant
  • Home
  • About me
  • Articles
  • Downloads
  • Contact me

Event Bubbling in AS2.0

7 01 2006

There’s been a lot of hype around the support for event bubbling in AS3 recently, and after Ralf Bokelberg posted an ingenious method to get it to work with AS2.0 thought I’d look at it more closely and post an example.

First of all, what exactly is event bubbling? Well the concept is relatively easy, an event dispatched from a nested view ‘bubbles’ up the chain to its parent views so they can listen to and act upon that event. Without event bubbling the parent views need a reference to the nested view to listen to subscribe to the events.

Let’s say we’ve got a movieclip mc1 that contains a nested movieclip mc2. When mc1.mc2 broadcasts an event, and you want to listen to it without event bubbling at your disposal you would write:

mc1.mc2.addEventListener("someEvent",someEventHandler); 

With event bubbling however you can simply use:

mc1.addEventListener("someEvent",someEventHandler);

as the event moves up the chain to parent movieclips and you don’t need to care or know anything about where the event actually originates from.

import mx.events.EventDispatcher;

// Ralf Bokelberg's AS2.0 event bubbling workaround
initializeBubbling = function(dispatcher:MovieClip) {
  var parentDispatcher = dispatcher._parent;
  var oldDispatchEvent = dispatcher.dispatchEvent;
  dispatcher.dispatchEvent = function( evt){
    oldDispatchEvent.call( dispatcher, evt);
      if( evt.bubbles){
        parentDispatcher.dispatchEvent(evt);
      }
    }
}

// create empty movieclips
var mc1:MovieClip = createEmptyMovieClip("mc1",1);
var mc2:MovieClip = mc1.createEmptyMovieClip("mc2",1);

// initialize EventDispatcher
EventDispatcher.initialize(mc1);
EventDispatcher.initialize(mc1.mc2);

// initialize event bubbling
initializeBubbling(mc1.mc2);

// add event listeners to movieclips
mc1.mc2.addEventListener("customEvent",function() {
  trace("customEvent captured by mc1.mc2");
});
mc1.addEventListener("customEvent",function() {
  trace("customEvent captured by mc1");
});

// dispatch event
mc1.mc2.dispatchEvent({type:"customEvent",bubbles:true});

In the code snippet above you’ll see Ralf’s event bubbling workaround in action and the “customEvent” event dispatched by mc1.mc2 captured by mc1. If you set the bubbles property to false when you dispatch the event mc1 will no longer capture the event. That’s pretty cool! You can see the advantages of not having to know anything about where the event comes from down the chain, if an event is broadcast and is set to bubble you’ll catch it.

« Happy New Year! Flash Thriller Night »



Actions

Informations

  • Date : 7 January 2006
  • Categories : Flash

6 responses to “Event Bubbling in AS2.0”

7 01 2006
Tom Ortega (23:57:58) :

This is some sweeeeet action right here. I have an app that goes down so many layers. I’ve had to hack my way through, using result methods, adding mc prototype functions, and hardcoded _parent._parent.etc. more than I’d like to admit.

Why couldn’t I think of this? Would’ve saved me sooo much heartache!

Well, now I know. Like G.I. Joe would say, “And knowing is half the battle.” =)

8 01 2006
reintroducing.com Blogging Receptacle » Blog Archive » Event Bubbling in AS 2.0 (10:03:50) :

[...] Peter Elst posted a blog about Event Bubbling in AS 2.0.  It’s a pretty good read so check it out. [...]

8 01 2006
Julian Sander (14:59:35) :

Hi Peter,

thanx for this! Hadn’t thought about trying this until I read this post. I wrapped the bubble idea in a subclass of EventDispatcher for those that are inerested. You can grab it at

http://abstractfactory.de/download.php?file=20

or just check the GPL file list on the main page of my site.

cheers, Julian

8 01 2006
Shaoken (15:24:51) :

Francis Bourre made a good implementation of event bubbling in his PixLib framework.

Check this:
http://svn.sourcesecure.co.uk/osflash/pixlib/trunk/framework/src/com/bourre/events/

Although, Francis thinks that this part must be refactored, it’s a very good start point.

Cheers,
Stéphane.

25 01 2006
Manvir Singh (11:25:09) :

Thanks a lot, Peter. I was facing a problem which is now quite logically solved using Bubbling. Its a pretty cool example, good work. Keep it up… :)

2 03 2006
kouri (17:50:00) :

Thx for this sample ..a little liter and clearer than the Ralf’s one ;-)
Bubbling seems to work for me as mc1 captures event from mc2.
But I just don’t catch why mc2 does not capture its own event…It should as it listen to it too.. Am I wrong ?





Conferences

Flash on the Beach Speaker

Training Partners

multimediacollege

SkillsMatter

Badges

Alltop, confirmation that I kick ass

  • Categories

    • AIR RSS category feed
    • Ajax RSS category feed
    • CSS RSS category feed
    • Eclipse RSS category feed
    • Events RSS category feed
    • Flash RSS category feed
    • Flash Lite RSS category feed
    • Flex RSS category feed
    • Gadgets RSS category feed
    • General RSS category feed
    • JSFL RSS category feed
    • Mac RSS category feed
    • Open Source RSS category feed
    • PHP RSS category feed
    • Podcasts RSS category feed
    • Publications RSS category feed
    • Rants RSS category feed
    • Reviews RSS category feed
    • Thought of the Day RSS category feed
    • Training RSS category feed
    • Twitter RSS category feed
    • Video RSS category feed
  • Resources

    • Stephen Downs
    • Matthew David
    • Veronique Brossier
    • Guy Watson
    • Scott Barnes
    • Burak Kalayci
    • Owen van Dijk
    • Peldi Guilizzoni
    • Richard Leggett
    • André Michelle
    • LordAlex Leon
    • Sas Jacobs
    • Simon Barber
    • Ralf Bokelberg
    • Matt Voerman
    • Edwin van Rijkom
    • Abdul Qabiz
    • Jon Williams
    • Sam Robbins
    • Mario Klingemann

     
     

    Adobe Community Expert

    See my profile on LinkedIn



    Harz Ferienwohnung Suchmaschinenoptimierung Geschenkideen Harz Ferienwohnung Pagerank Webkatalog Webhosting