监听子组件中显示列表中的事件
我正在创建一个 swf,它有一个父类和一个子类。父类有一个按钮,用于调度自定义事件,我希望子类列出该事件,但是当我调度该事件时,子类听不到该事件已被调度。
这是调度事件的代码:
private function onCTAClicked(e:MouseEvent):void
{
trace("onCTAClicked");
dispatchEvent(new CTAClickEvent(CTAClickEvent.CTA_CLICK_EVENT,true));
}
And the listener is registered like this:
public function registerEventListeners():void
{
this.addEventListener(CTAClickEvent.CTA_CLICK_EVENT, onCTAClickHandler,false);
}
registerEventListeners() 函数位于子类中。
我知道事件可以在显示列表中向上冒泡,但是如何才能在列表中向下移动呢?
斯蒂芬
I'm creating a swf, that has a parent class and a child class. The parent class has a button, that dispatches a custom event and I want the child class to list for this event, but when I dispatch the event the child class does not hear the event has been dispatched.
This is the code that dispatches the event:
private function onCTAClicked(e:MouseEvent):void
{
trace("onCTAClicked");
dispatchEvent(new CTAClickEvent(CTAClickEvent.CTA_CLICK_EVENT,true));
}
And the listener is registered like this:
public function registerEventListeners():void
{
this.addEventListener(CTAClickEvent.CTA_CLICK_EVENT, onCTAClickHandler,false);
}
The registerEventListeners() function is in the child class.
I know events can bubble up the display list but how can then go down the list?
Stephen
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
不,事件不会深入下去。它们只会冒泡。为了使显示对象的子对象能够听到父对象分派的事件,在子对象的类中,您需要向父对象的引用添加侦听器。
请确保当parent 可能为空时不要调用registerEventListeners。
No, events do not dig down. They only bubble up. In order for a child of a display object to hear an event dispatched by a parent, in the class of the child object you'll need to add a listener to a reference of the parent object.
Just be sure not to invoke registerEventListeners when parent might be null.