如何监听发送到某个对象的所有事件?

时间:2013-01-07 11:09:48

标签: flash flex events

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

2 个答案:

答案 0 :(得分: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:

[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]

答案 1 :(得分: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);
    }
}