このクラスは、その問題に対する私の解決策でした。基本的に、BubblingEventDispatcher を拡張する代わりに、通常は EventDispatcher を拡張するクラスを用意します。
次に addChildTarget( target:BubblingEventDispatcher ) 関数を呼び出して、バブルされたイベントをキャッチできる子をセットアップします。
このソリューションでは、イベント ディスパッチャごとにスプライトを使用しますが、クラスごとに 1 バイトの余分なメモリ使用量しか発生しません。
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.events.EventDispatcher;
public class BubblingEventDispatcher extends EventDispatcher
{
//We have to use a sprite to take advantage of flash's internal event bubbling system
private var sprite:Sprite;
public function BubblingEventDispatcher()
{
//initialize our sprite
sprite = new Sprite();
}
public override function addEventListener(type:String, listener:Function, useCapture:Boolean=false, priority:int=0, useWeakReference:Boolean=false):void
{
sprite.addEventListener( type, listener, useCapture, priority, useWeakReference );
}
public override function dispatchEvent(event:Event):Boolean
{
return sprite.dispatchEvent( event );
}
//We must add child targets if we want to take advantage of the bubbling
public function addChildTarget( target:BubblingEventDispatcher ):void
{
sprite.addChild( target.eventTarget as Sprite );
}
public function get eventTarget():EventDispatcher
{
return sprite;
}
}
}