开发者

accessing function on stage from a child

开发者 https://www.devze.com 2023-01-08 03:09 出处:网络
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();

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();
0

精彩评论

暂无评论...
验证码 换一张
取 消