actionscript-3eventsdisplaylist

listening to events down the display list in children components


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


Solution

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