0

addEventListener如果将特定类型的事件分派给某个对象,则使用I 可以注册要调用的回调。如果将任何事件分派给对象,我如何调用我的回调?

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 回答