flash的dispatchEvent到底是如何工作的?

发布于 2024-10-16 22:09:16 字数 459 浏览 8 评论 0原文

文档,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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

寂寞陪衬 2024-10-23 22:09:16

这完全取决于您的显示列表层次结构。

Flash 的事件结构基于其 内部事件模型

  1. 舞台将是第一个对象
    通知,然后该事件将
    向下滴流显示列表直到
    它达到了目标。这个阶段是
    称为捕获阶段。要启用它,请将 useCapture 设置为
    事件侦听器上的 true。请注意
    这样做是没有意义的,除非
    监听的对象是以下对象的父对象
    针对事件的对象。这
    称为事件拦截

  2. 下一阶段是目标
    阶段。这是最常见的行为
    众所周知的事件。这
    目标显示对象(
    有一个事件监听器)将
    接收事件并执行
    侦听器中的代码。

  3. 最后一个阶段称为
    冒泡阶段。这是事件在显示列表中冒泡的时候
    收到事件之后。事件冒泡对于
    分派自定义事件,如您所愿
    需要知道如何倾听
    由对象调度的事件
    孩子们。

分派事件时,我通常使用以下语法(Event.CHANGE 只是一个常见示例):

Object.dispatchEvent(new Event("CHANGE", true, false));

Object 是您从中分派的对象。第一个参数是您要分派的事件。第二个是“bubbles”参数。最后一个是cancelable 属性。 Event.cancelable 用于通过 Event.preventDefault() 阻止事件的默认操作(IE:鼠标单击)。

参考:

It all depends on your display list hierarchy.

Flash's event structure is based on its internal event model.

  1. 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 to
    true on an event listener. Do note
    that it's pointless to do so unless
    the object listening is a parent of
    the object targeting the event. This
    is called event intercepting.

  2. 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.

  3. 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 the bubbles parameter. The final is the cancelable property. Event.cancelable is used to prevent the default action of an event (IE: a mouse click) via Event.preventDefault().

Reference:

自此以后,行同陌路 2024-10-23 22:09:16

只需使用信号即可: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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文