Event Bubbling in AS2

Yes, we all know the numbers. FlashPlayer 9 is at 97% penetration worldwide. After a year of primarily AS3 development (not starting any new AS2 projects), I find myself working on a project back-porting AS3 into AS2. Ohhhhhh yay, what a joy! I love the screwy event model, the rigid display list, having to fake document classes, not being able to do new MovieClip(), … and the list goes on… Regardless, this is my task, and I will prevail with flying colors (are you with me?) !

One requirement to keeping the code bases similar is to implement event bubbling in AS2. I looked around for a few techniques we could implement into our ‘new’ AS2 code, but had trouble finding an example that could be added cleanly into our project. We needed something that could extend MovieClip, and allow any subclass to bubble events up their respective _parent chain. I definitely found some working bubbling techniques, mainly this one, but decided to re-familiarize myself with AS2 by encapsulating this whole bubbly stuff.

I came to a solution I am happy with. Some things like the capture phase are not implemented, because I don’t think we need them in out back-port, but bubbling works and propagation can be stopped.

I ended up creating a basic Event class and an abstract BaseViewObject class with a bubbleEvent method. I really wanted to extend the dispatchEvent method of the EventDispatcher class, but damn, I could not get that to work for the life of me… so I just defined a new method.

import mx.events.EventDispatcher;
import com.greenethumb.events.Event;

class com.greenethumb.display.BaseViewObject extends MovieClip 
{

	/*
     * Contructor
     * 
     * Initializes the event dispatcher
     */
    public function BaseViewObject() 
    {
        EventDispatcher.initialize( this );
    }

	/*
     *  Takes care of sending the event up through the display list. The event needs to have
     *   its bubble property set to true, or it will not be sent up the display list
     */
    public function bubbleEvent( evt:Event ) : Void
    {
        // set the events target (will only be set once during the dispactch/bubbling phase)    
        if( evt.target == undefined || evt.target == null)
        {
            evt.target = this;  
        }

	// set the current target - the MovieClip that is processing the event
        evt.currentTarget = this._parent;

	// send out the event
        dispatchEvent( evt );

	// check to see if the event wants to bubble, and if we are not the top of our allowed display list
        if( evt.bubbles && this != _root)
        {
            // bubble to our parent
            BaseViewObject( this._parent ).bubbleEvent( evt );
        }
        else if (this == _root)
        {
            // we are at the top...
        }
        else
        {
            // let the user no that an event has been stopped
            trace( 'Event [' + evt.type + '] originating from [' + evt.target + '] had its bubbling stopped by [' + evt.currentTarget + ']');   
        }
    }

	// need to be here for AS2 EventDispatcher... yuck
    public function dispatchEvent() : Void {};
    public function addEventListener() : Void {};
    public function removeEventListener() : Void {};
}

I am also posting my example FLA and classes to show you how it works. You can download the AS2 event bubbling source/example files here .

If you are interested in this, then we are brethren is the AS2 jungle… good luck, and keep your machete sharp!

One Response to 'Event Bubbling in AS2'

  1. utirulsesek says:

    åªã å¬å¼å°å£²åº ãªãªã¸ãã« ããã¿ãª åºè ä¸çæé«å³° æã [url=http://www.jplouboutinkutuoutlet.com/ã«ãã¿ã³ãã¼ãã¤ã¾å-outlet-11.html]ã¯ãªã¹ãã£ã³ã«ãã¿ã³ åºè[/url] å質ä¿è¨¼ èä¹æ§ åªã çå° ã®è¯ãå質 å¤§å¤ ã·ã§ã«ãã¼ããã° æ群
    ãªããã¥ããã£ã¼ 㪠ããã㦠ã»ããªã㯠ãããã ã­ãã¸ã³ã° ããã ããã´ã ããã° ããããã ããããã [url=http://www.jplouboutinkutuoutlet.com/]ã«ãã¿ã³ é´[/url] ã¿ããã³ ã¨ãã« ãã©ã¡ã ããã¾ãã ãµãã¡ ã³ã­ã©ã ããã­ã ã²ãã ã²ã¨ãã²ã¨ã ã¾ããã¨ã
    ç´«è² ã㨠æµè¡ãã é«å質 ã¦ã¼ã« [url=http://giuseppezanottikutu.com/]ã¸ã¥ã¼ããã¶ããã㣠ãµã¤ãºæ[/url] å»ä¾¡ ã¡ãªã¤ã¹ æ¿å®é販 ãã£ãããã é常価格 æ°ä½ æ°ç æ¿å®
    ãªã¼ã¬ãªãã¤ã·ã³ ããã ãããã ãã©ã¯ ã´ã¼ã«ã ã¹ã¿ã³ãã¼ã ã¯ããã ããããã㨠ãã«ãã¡ã¼ã¿ã¼ ãªã ã·ã§ã«ãã¼ [url=http://giuseppezanottikutu.com/ã¸ã¥ã¼ããã¶ãããã£ãã¤ãã¼ã«-ã¸ã¥ã¼ããã¶ãããã£ã¦ã§ãã¸-outlet-1_7.html]ã¸ã¥ã¼ããã¶ããã㣠æ¿å®[/url] ã«ã¬ã㸠ã¹ã¿ã¤ã« ãã¡ããã ãã©ãã¯ã¹ ããã ãã¨ã ã¼ããã ãããã ãããã ãµãã ãããã¤
    ã¯ãªã¹ãã¹ å¸å° å®ç¨æ§ãé«ã ææ°ç»å ´ ãã¤ã­ã³ [url=http://www.christianlouboutinkutu2013.com/ã«ãã¿ã³-æ°ä½-outlet-12.html]ã«ãã¿ã³[/url] ããã å¿«é©ã æ°ã¢ãã« ä¿æ¸©æ§ ã® ä¸è³ª è¯ãå質㮠é²å¯ æãã å¨ã¦
    ãã¨ããã ã¸ãã ããããããã³ãã ãã¹ã¦ ã¯ã¨ã¼ã«ã¼ ã¤ããã ã ãã¸ã£ã ãããã®ã»ã© ãã£ãã [url=http://www.christianlouboutinkutu2013.com/ã«ãã¿ã³-daffodile-outlet-1.html]ã«ãã¿ã³ é販[/url] ãããã ããã ã㢠ãªãªã㯠ã¿ãªã¨ ã¹ã¤ãã ã²ã£ã±ã ããã¼ã ã¼ãã ãã³ã ããããããããã¤

Leave a Reply to utirulsesek Cancel reply

Your email address will not be published. Required fields are marked *

*