flash的dispatchEvent到底是如何工作的?
文档,EventDispatcher 的dispatchEvent“...将事件分派到事件流中”。这句话看起来很好看,但并没有真正解释什么。
时我们需要期待什么行为
假设我们有两个侦听器等待对象“a”上的事件“A”,那么调用a.dispatchEvent(“A”)
?在从 distpatchEvent 返回之前,是否会立即调用两个侦听器?或者它们将在某些内部 Flash 播放器队列中排队并通过进入下一帧进行处理?我们可以在这里依赖 flash 播放器的某些定义的行为还是未定义的行为?应该如何理解“将事件分派到事件流”?这个问题很重要,因为在实践中它会影响代码的控制流。
It is said in the docs, that EventDispatcher's dispatchEvent "...dispatches an event into the event flow". The phrase is nice-looking and doesn't really explain anything.
Say, we have two listeners waiting for an event "A" on object "a", so what behaviour do we have to expect on calling:
a.dispatchEvent("A")?
Would both listeners be called immediately, before return from distpatchEvent? Or they will be queued in some internal flash player queue and will be processed by entering the next frame? Can we rely on some defined behaviour of flash player here or the behaviour is undefined? How one should read "dispatches an event to event flow"? The question is important since in practice it affects the control flow of the code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这完全取决于您的显示列表层次结构。
Flash 的事件结构基于其 内部事件模型。
舞台将是第一个对象
通知,然后该事件将
向下滴流显示列表直到
它达到了目标。这个阶段是
称为捕获阶段。要启用它,请将
useCapture
设置为事件侦听器上的
true
。请注意这样做是没有意义的,除非
监听的对象是以下对象的父对象
针对事件的对象。这
称为事件拦截。
下一阶段是目标
阶段。这是最常见的行为
众所周知的事件。这
目标显示对象(
有一个事件监听器)将
接收事件并执行
侦听器中的代码。
最后一个阶段称为
冒泡阶段。这是事件在显示列表中冒泡的时候
在收到事件之后。事件冒泡对于
分派自定义事件,如您所愿
需要知道如何倾听
由对象调度的事件
孩子们。
分派事件时,我通常使用以下语法(
Event.CHANGE
只是一个常见示例):Object.dispatchEvent(new Event("CHANGE", true, false));
Object
是您从中分派的对象。第一个参数是您要分派的事件。第二个是“bubbles”参数。最后一个是cancelable
属性。Event.cancelable
用于通过Event.preventDefault()
阻止事件的默认操作(IE:鼠标单击)。参考:
基本 Actionscript 3.0
It all depends on your display list hierarchy.
Flash's event structure is based on its internal event model.
The Stage will be the first object
notified, and then the event will
trickle down the display list until
it reaches its target. This phase is
called the capture phase. To enable it, set
useCapture
totrue
on an event listener. Do notethat it's pointless to do so unless
the object listening is a parent of
the object targeting the event. This
is called event intercepting.
The next phase is the target
phase. This is the behavior most
commonly known with events. The
targeted display object (the one the
has a listener for the event) will
receive the event and carry out the
code in the listener.
The final phase is called the
bubbling phase. This is when the event bubbles up the display list
after the event has been received. Event bubbling is very important for
dispatching custom events, as you'll
need to know how to listen for
events dispatched by an object's
children.
When dispatching an event, I generally use this syntax (
Event.CHANGE
is just a common example):Object.dispatchEvent(new Event("CHANGE", true, false));
The
Object
is the object you're dispatching from. The first parameter is the event you're dispatching. The second is thebubbles
parameter. The final is thecancelable
property.Event.cancelable
is used to prevent the default action of an event (IE: a mouse click) viaEvent.preventDefault()
.Reference:
Essential Actionscript 3.0
只需使用信号即可:P
https://github.com/robertpenner/as3-signals/wiki
不,但实际上,它们非常易于使用和理解,是对 AS3 工具箱的一个很好的补充。
您还可以通过阅读 Rob Penner 的评论(向下滚动到 wiki 页面底部)了解更多有关本机 AS3 事件如何工作的信息
Just use Signals instead :P
https://github.com/robertpenner/as3-signals/wiki
No but really, they're very easy to use and understand, a great addition to the AS3 toolbox.
You can also learn a lot about how native AS3 events work by reading Rob Penner's critiques (scroll down to bottom of wiki page)