0

I を使用addEventListenerすると、特定の種類のイベントが何らかのオブジェクトにディスパッチされた場合に呼び出されるコールバックを登録できます。イベントがオブジェクトにディスパッチされた場合、コールバックを呼び出すにはどうすればよいですか?

4

2 に答える 2

2

dispatchEvent次のようにオーバーライドできます。

public class UniversalDispatcher extends EventDispatcher {

    override public function dispatchEvent(event:Event):Boolean {
        if (event is UniversalEvent)
            return false;
        var uEvent:UniversalEvent = new UniversalEvent();
        uEvent.subEvent = event.clone();
        var res:Boolean =  super.dispatchEvent(event);
        super.dispatchEvent(uEvent);
        return res;
    }
}

オリジナル イベントを含むカスタム イベントを作成します。

public class UniversalEvent extends Event {

    public static const UNIVERSAL:String = "universal";
    public var subEvent:Event;

    public function UniversalEvent(type:String = UNIVERSAL, bubbles:Boolean = true, cancelable:Boolean = false) {
        super(type, bubbles, cancelable);
    }
}

使用例:

var ud:UniversalDispatcher = new UniversalDispatcher();
ud.addEventListener(UniversalEvent.UNIVERSAL, onUniversalEvent);
ud.dispatchEvent(new Event("blabla"));
ud.dispatchEvent(new Event("test"));
ud.dispatchEvent(new MouseEvent(MouseEvent.CLICK));

private function onUniversalEvent(event:UniversalEvent):void {
    trace("Event dispatched: " + event.subEvent.toString());
}

痕跡:

[trace] Event dispatched: [Event type="blabla" bubbles=false cancelable=false eventPhase=2]
[trace] Event dispatched: [Event type="test" bubbles=false cancelable=false eventPhase=2]
[trace] Event dispatched: [MouseEvent type="click" bubbles=true cancelable=false eventPhase=2 localX=NaN localY=NaN stageX=NaN stageY=NaN relatedObject=null ctrlKey=false altKey=false shiftKey=false buttonDown=false delta=0]
于 2013-01-07T12:07:49.250 に答える
0

「addEventListener」で追加された各イベントリスナーを制御できる場合は、次のようにすることができます。

import flash.events.Event;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.utils.Dictionary;

var label:TextField;
var wrap:Wrapper;

label.addEventListener(MouseEvent.CLICK, wrap.universalFunc( MouseEvent.CLICK, onLabelClick ));
label.addEventListener(MouseEvent.MOUSE_DOWN, wrap.universalFunc( MouseEvent.MOUSE_DOWN, onLabelMouseDown ));
label.addEventListener(MouseEvent.MOUSE_UP, wrap.universalFunc( MouseEvent.MOUSE_UP, onLabelMouseUp ));


class Wrapper {
    private var _eventTypes:Dictionary = new Dictionary();

    public function universalFunc( pEventType:String, pCallbackFunc:Function ):Function {
        _eventTypes[pEventType] = pCallbackFunc;
    }

    private function universalListener(e:Event):void {
        var callback:Function = _eventTypes[e.type];
        if (!callback) {
            return;
        }

        //Do whatever common behavior you want here:
        // ...

        //Pass the event to the callback method:
        callback(e);
    }
}

または、はるかにクリーンで冗長性が低い:

import flash.events.Event;
import flash.events.IEventDispatcher;
import flash.text.TextField;
import flash.events.MouseEvent;
import flash.utils.Dictionary;

var label:TextField;
var wrap:Wrapper;

wrap.listen( label, MouseEvent.CLICK, onLabelClick );
wrap.listen( label, MouseEvent.MOUSE_DOWN, onLabelMouseDown );
wrap.listen( label, MouseEvent.MOUSE_UP, onLabelMouseUp );

class Wrapper {
    private var _eventTypes:Dictionary = new Dictionary();

    public function listen( pDispatcher:IEventDispatcher, pEventType:String, pCallbackFunc:Function ):void {
        _eventTypes[pEventType] = pCallbackFunc;

        pDispatcher.addEventListener(pEventType, universalListener);
    }

    private function universalListener(e:Event):void {
        var callback:Function = _eventTypes[e.type];

        if (!callback) {
            return;
        }

        //Do whatever common behavior you want here:
        // ...

        //Pass the event to the callback method:
        callback(e);
    }
}
于 2013-01-07T17:59:36.137 に答える