任何对象要观察另一个对象派发的事件,有几个条件:
- 该对象必须是显示对象。
- 调度该事件的对象必须是侦听对象的子对象。
如果父对象正在侦听冒泡阶段(默认),则在事件创建期间必须将冒泡设置为 true。
dispatchEvent(new Event(TYPE_NAME, true));
否则它可以在捕获阶段被捕获,添加事件监听器'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"
}
}
}
我很高兴能对这个主题有更多的了解。