listening to events down the display list in children components

最后都变了- 提交于 2020-01-07 02:51:26

问题


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


回答1:


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);
}

Just be sure not to invoke registerEventListeners when parent might be null.



来源:https://stackoverflow.com/questions/6495037/listening-to-events-down-the-display-list-in-children-components

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!