场景图形/显示列表之外的 AS3 事件冒泡 [英] AS3 Event Bubbling outside of the Scenegraph/DisplayList

查看:23
本文介绍了场景图形/显示列表之外的 AS3 事件冒泡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想知道是否可以在 AS3 中的非显示列表类中利用事件冒泡.

Hi just wondering if it is possible to take advantage of event bubbling in non display list classes in AS3.

例如在一个应用程序模型中,其中有一个包含许多汽车的 City 类.有什么方法可以将事件侦听器附加到 City 对象并接收从子 Cars 冒泡的事件.澄清一下 City 和 Car 对象不是显示列表的一部分,它们不是 DisplayObjects.那么冒泡可以以某种方式在显示列表之外实现吗?

For example in the model of an application where there is a City class that contains many Cars. What methods are there to attach an event listener to a City object and receive events that bubble up from the child Cars. To clarify The City and Car objects are not part of the display list, they are not DisplayObjects. So can bubbling be implemented outside of the display list somehow?

据我所知,如果不手动将事件侦听器附加到每个 Car 对象并从 City 对象重新调度事件,这是不可能的.其他人有更清洁的解决方案吗?

As far as I know, this is not possible without manually attaching event listeners to each Car object and re dispatching the event from the City object. Anyone else have a cleaner solution?

推荐答案

我不确定我是否完全理解,但是当你创建一个事件时你可以说它是冒泡的,所以当它调度时它会冒泡.

I'm not sure I am fully understanding, but when you create an event you can say that it bubbles and so when it dispatches it will bubble.

>

http://livedocs.adobe.com/flex/3/html/help.html?content=createevents_3.html

http://livedocs.adobe.com/flex/3/langref/flash/events/Event.html#Event()

我相信这将允许您将事件侦听器附加到您的城市以获取正确类型的事件,并且可以捕获汽车抛出的事件.我从来没有尝试过这个,所以我不确定.

I believe that would allow you to attach an event listener to your city for the right types of events and that would catch events thrown by cars. I haven't ever tried this though so I am not sure.

更新:

啊,我没有意识到这一点.你是对的.来自以下链接:

Ah, I didn't realize this. You are correct. From the following link:

http://livedocs.adobe.com/flex/3/html/help.html?content=events_08.html

当 Event 对象在显示列表中从一个节点移动到另一个节点时会发生捕获和冒泡:父对子用于捕获,子对父用于冒泡.此过程与继承层次结构无关.除了定位阶段,只有 DisplayObject 对象(容器和控件等可视对象)可以有捕获阶段和冒泡阶段.

Capturing and bubbling happen as the Event object moves from node to node in the display list: parent-to-child for capturing and child-to-parent for bubbling. This process has nothing to do with the inheritance hierarchy. Only DisplayObject objects (visual objects such as containers and controls) can have a capturing phase and a bubbling phase in addition to the targeting phase.

我能看到的唯一方法是让您在每次添加新孩子时向父母(城市的汽车)注册孩子.在这一点上,您可以在父(城市)中添加一个事件侦听器,然后在从子(汽车)处理事件时从父(城市)重新调度事件.我知道这很丑陋,并且您希望确保在删除孩子的任何时候都删除事件侦听器,但这是我能看到的唯一真正的解决方案.

The only way I can see around this would be for you to register the child with the parent (the car with the city) every time a new one is added as a child. At that point you could add an event listener in the parent (city) and then re-dispatch an event from the parent (city) any time a event is handled from the child (car). Its ugly I know, and you would want to make sure you remove the event listener anytime that you remove a child, but it's the only real solution I can see.

这篇关于场景图形/显示列表之外的 AS3 事件冒泡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆