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...
            // 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

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