views:

19

answers:

1

Hi there,

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 is in the main stage. // I tried // parent.parentFunction(); // and // root.parentFunction(); but nothing works... }

thanks in advance...

A: 
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
{

}
Allan
fixed a little mistake I made in my second example
Allan