0

我不完全了解actionscript中的事件机制,所以如果问题被转储,请原谅。假设我们有一个Controller(extends EventDispatcher) 和几个视图,请注意视图可以是各种类的实例。假设每个视图都在监听CustomEvent.SOMETHING_CHANGED。问题是如果我这样做dispatchEvent(new CustomEvent(CustomEvent.SOMETHING_CHANGED))Controller事件是否会出现在每个听众身上,或者它会被第一个观点捕捉到并且不会更进一步。

先感谢您!

4

2 回答 2

2

是的,它将被传递给所有侦听器,直到其中一个调用事件对象的 stopImmediatePropagation 方法(这只会在事件可取消时停止事件)

于 2012-09-25T14:30:14.003 回答
1

任何对象要观察另一个对象派发的事件,有几个条件:

  1. 该对象必须是显示对象。
  2. 调度该事件的对象必须是侦听对象的子对象。
  3. 如果父对象正在侦听冒泡阶段(默认),则在事件创建期间必须将冒泡设置为 true。

            dispatchEvent(new Event(TYPE_NAME, true));
    
  4. 否则它可以在捕获阶段被捕获,添加事件监听器'useCapture = true 来完成这个。

            addEventListener(TYPE_NAME, onFuncName, true);
    

在所有其他情况下,对象只能在目标阶段捕获由对象本身调度的事件。

这是一个代码示例:

package regression 
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.events.EventPhase;
    /**
     * ...
     * @author ...
     */
    public class Check_event_listening_1 extends Sprite
    {
        public  const   EVENT_DANCE     : String    = "dance";
        public  const   EVENT_PLAY      : String    = "play";
        public  const   EVENT_YELL      : String    = "yell";
        private var     baby            : Shape     = new Shape();
        private var     mom             : Sprite    = new Sprite();
        private var     stranger        : EventDispatcher = new EventDispatcher();

        public function Check_event_listening_1() 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);
        }

        private function init(e:Event = null):void 
        {
            trace("test begun");
            addChild(mom);
            mom.addChild(baby);
            stage.addEventListener(EVENT_YELL, onEvent);
            this.addEventListener(EVENT_YELL, onEvent);
            mom.addEventListener(EVENT_YELL, onEvent);
            baby.addEventListener(EVENT_YELL, onEvent);
            stranger.addEventListener(EVENT_YELL, onEvent);
            trace("\nTest1 - Stranger yells with no bubbling");
            stranger.dispatchEvent(new Event(EVENT_YELL, false));
            trace("\nTest2 - Stranger yells with bubbling");
            stranger.dispatchEvent(new Event(EVENT_YELL, true));

            stage.addEventListener(EVENT_PLAY, onEvent);
            this.addEventListener(EVENT_PLAY, onEvent);
            mom.addEventListener(EVENT_PLAY, onEvent);
            baby.addEventListener(EVENT_PLAY, onEvent);
            stranger.addEventListener(EVENT_PLAY, onEvent);
            trace("\nTest3 - baby plays with no bubbling");
            baby.dispatchEvent(new Event(EVENT_PLAY, false));
            trace("\nTest4 - baby plays with bubbling");
            baby.dispatchEvent(new Event(EVENT_PLAY, true));
            trace("\nTest5 - baby plays with bubbling but is not a child of mom");
            mom.removeChild(baby);
            baby.dispatchEvent(new Event(EVENT_PLAY, true));
            mom.addChild(baby);

            stage.addEventListener(EVENT_DANCE, onEvent, true);
            this.addEventListener(EVENT_DANCE, onEvent, true);
            mom.addEventListener(EVENT_DANCE, onEvent, true);
            baby.addEventListener(EVENT_DANCE, onEvent, true);
            trace("\nTest6 - Mom dances without bubbling - everyone is listening during capture phase(not target and bubble phase)");
            mom.dispatchEvent(new Event(EVENT_DANCE, false));
            trace("\nTest7 - Mom dances with bubbling - everyone is listening during capture phase(not target and bubble phase)");
            mom.dispatchEvent(new Event(EVENT_DANCE, true));
        }

        private function onEvent(e : Event):void
        {
            trace("Event was captured");
            trace("\nTYPE : ", e.type, "\nTARGET : ", objToName(e.target), "\nCURRENT TARGET : ", objToName(e.currentTarget), "\nPHASE : ", phaseToString(e.eventPhase));
        }

        private function phaseToString(phase : int):String
        {
            switch(phase)
            {
                case EventPhase.AT_TARGET :
                    return "TARGET";
                case EventPhase.BUBBLING_PHASE :
                    return "BUBBLING";
                case EventPhase.CAPTURING_PHASE :
                    return "CAPTURE";
                default:
                    return "UNKNOWN";
            }

        }

        private function objToName(obj : Object):String
        {
            if (obj == stage) return "STAGE";
            else if (obj == this) return "MAIN";
            else if (obj == mom) return "Mom";
            else if (obj == baby) return "Baby";
            else if (obj == stranger) return "Stranger";
            else return "Unknown"
        }

    }

}

我很高兴能对这个主题有更多的了解。

于 2012-09-25T16:41:54.973 回答