I have a function in stage and need to call it from a child I do I do it...
// In main stage
var child_mc:mcChild = new mcChild();
addChild(child_mc);
function parentFunction():void
{
trace("Do something here");
}
// inside mcChild
button_mc.addEventListener(MouseEvent.CLICK, callParentFunction);
function callParentFunction(e:MouseEvent):void
{
// here I need to call the function that i开发者_开发知识库s in the main stage.
// I tried
// parent.parentFunction();
// and
// root.parentFunction(); but nothing works...
}
child_mc.addEventListener(MouseEvent.CLICK, onParentFunction)
onParentFunction(e:MouseEvent)
{
parentFunction();
}
Whenever you need to call a function on a parent class then use events. If you were not clicking on a child you can do something similar by dispatching an event. Simply:
In child_MC
dispatchEvent(new Event("SOME_EVENT"));
Parent
child_mc.addEventListener("SOME_EVENT",onSomeEvent);
function onSomeEvent(e:Event):void
{
}
here :
// stage movie
MovieClip(stage.getChildAt(0)).someFunction();
精彩评论