在 Actionscript 3 中,大多数事件使用现在非常流行的捕获/目标/气泡模型:
When an event occurs, it moves through the three phases of the event flow: the capture phase, which flows from the top of the display list hierarchy to the node just before the target node; the target phase, which comprises the target node; and the bubbling phase, which flows from the node subsequent to the target node back up the display list hierarchy.
但是,某些事件,例如 Sprite 类的
enterFrame
事件,不要捕获或冒泡 - 您必须直接订阅目标才能检测事件。文档将这些称为“广播事件”。我认为这是出于性能原因,因为舞台上的每个 Sprite 都会不断触发这些事件,并且您不想处理所有多余的事件传播。我想发送我自己的广播事件。我知道您可以防止事件冒泡(
Event.bubbles = false
),但是您也可以摆脱捕获吗?
最佳答案
back2dos 的答案是 错了 .其实Event.bubbles
属性不影响捕获阶段。
public class CaptureTextInput extends Sprite
{
function CaptureTextInput()
{
var t:TextField = new TextField();
t.type = TextFieldType.INPUT;
addEventListener(TextEvent.TEXT_INPUT, function(event:TextEvent):Void
{
trace("captured"); // This event will be triggered properly when you type in text field.
}, true);
addChild(t);
}
}
关于flash - AS3 中的自定义广播事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2445377/