使用事件更新多个视图之一

时间:2012-09-25 14:26:04

标签: actionscript-3 flash events

我没有完全处理动作中的事件机制,所以如果问题是转储,请原谅。假设我们有Controller(扩展EventDispatcher)和几个视图,请注意视图可以是各种类的实例。假设每个视图都侦听CustomEvent.SOMETHING_CHANGED。问题是如果我在dispatchEvent(new CustomEvent(CustomEvent.SOMETHING_CHANGED))Controller,每个听众都会遇到一个事件,或者它会被第一个观点捕获,而不会更进一步。

提前谢谢!

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

对于任何观察由另一个对象调度的事件的对象,有几个条件:

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

            dispatchEvent(new Event(TYPE_NAME, true));
    
  4. 否则可以在Capture阶段捕获它,使用'useCapture = true添加事件监听器来实现此目的。

            addEventListener(TYPE_NAME, onFuncName, true);
    
  5. 在所有其他情况下,对象只能捕获目标阶段中对象本身调度的事件。

    这是一个代码示例:

    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"
            }
    
        }
    
    }
    

    我很乐意更多地了解这个问题。