[flash] Elegant Way to use Weak Listeners

Utility Class

package mypackage;

import flash.events.IEventDispatcher;

class WeakListeners
    public static /*inline*/ function addWeakListener(d:IEventDispatcher, type:String, 
                                    listener: Dynamic -> Void, ?useCapture:Bool = false, ?priority:Int = 0):Void
        d.addEventListener(type, listener, useCapture, priority, true);


package mypackage;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.Lib;

using mypackage.WeakListeners;

class Main 
    public static function main () 
        var sprite = new Sprite();
        var g = sprite.graphics;
        g.drawRect(0, 0, 100, 100);
        sprite.addWeakListener(MouseEvent.CLICK, function (e) trace("click"));

i think it's clearer and more readable than writing:

sprite.addEventListener(type, listener, false, 0, true);

it also decreases the possibility to forget using weak references.

version #7026, modified 2009-09-19 02:06:53 by tylermac