Friday 3 June 2011

AS3 - Child listening to Parent Dispatches

To get a child object to listen to events dispatched by the parent the parent dispatches an event which bubbles and the child listens for the event on the stage.


Child Code:


if (stage)
{
init ();
} else
{
addEventListener (Event.ADDED_TO_STAGE, init);
}
function init (evt:Event = null):void
{
removeEventListener (Event.ADDED_TO_STAGE, init);
stage.addEventListener("messageToChild", messageFromParent)
//trace("init _");
}

function messageFromParent(event:Event){
trace("MESSAGE from parent - go to CTA");
gotoAndStop("CTA");
}



Parent Code:


function playCTA():void {
ExpandingComponent.collapse();
dispatchEvent(new Event("messageToChild", true))
;

}

2 comments: