听下来子组件的显示列表事件 [英] listening to events down the display list in children components
问题描述
我创建一个SWF,有一个父类和子类。父类有一个按钮,即调度自定义事件,我想在子类列出了此事件,但是当我分派事件的子类没有听到该事件已经出动。
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.
这是在code分派事件:
This is the code that dispatches the event:
私有函数onCTAClicked(E:MouseEvent)方法:无效 { 跟踪(onCTAClicked); 则dispatchEvent(新CTAClickEvent(CTAClickEvent.CTA_CLICK_EVENT,真)); }
private function onCTAClicked(e:MouseEvent):void { trace("onCTAClicked"); dispatchEvent(new CTAClickEvent(CTAClickEvent.CTA_CLICK_EVENT,true)); }
和注册该侦听器是这样的:
And the listener is registered like this:
公共职能registerEventListeners():无效
{
this.addEventListener(CTAClickEvent.CTA_CLICK_EVENT,onCTAClickHandler,假);
}
public function registerEventListeners():void
{
this.addEventListener(CTAClickEvent.CTA_CLICK_EVENT, onCTAClickHandler,false);
}
在registerEventListeners()函数在子类。
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?
斯蒂芬
推荐答案
没有,事件不会往下挖。他们只泡了起来。为了显示对象的子听取家长调度的事件,在类的子对象的你需要一个侦听器添加到父对象的引用。
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.
public function registerEventListeners() : void {
parent.addEventListener(CTAClickEvent.CTA_CLICK_EVENT, onCTAClickHandler);
}
只要确保不调用registerEventListeners时,父母可能为空。
Just be sure not to invoke registerEventListeners when parent might be null.
这篇关于听下来子组件的显示列表事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!