AS3 - How to call a function in the root from a nested - dynamically loaded movieclip?
Does anyone know the best method to trigger a functi开发者_JAVA百科on in the root from a dynamically loaded movieclip (loaded using addchild) using AS3, I understand targeting root is not the best way to do this?
Thanks Paul
DanielB has a point, this is not really the best design... The object calling the function can be set when the movie is loaded. This can be done in many ways, for instance...
private var _functionCaller:MovieClip;
private function onLoadComplete(event:Event):void
{
_functionCaller = this.parent; // or this.parent.parent or whatever else
}
private function callExternalFunction():void
{
if( _functionCaller != null )
_functionCaller.execute();
}
A better approach could be to use an event to inform your root to call a function...
private var _dispatcher:EventDispatcher;
//this function will be called from the parent
public function init(dispatcher:EventDispatcher):void
{
_dispatcher = dispatcher
}
private function callExternalFunction():void
{
// You could also create a Custom Event
if( _dispatcher != null )
_dispatcher.dispatchEvent("call external function");
}
You should think about your concept. Your loaded SWF should call a function from the movie it is loaded in to work properly? so the loaded movie will never run standalone?
You should implement all functionality in your loader (parent) swf. The loader movie knows what he is loading so maybe he can call a function on a loaded clip. But the other way around is simply bad design.
You can react on COMPLETE
(loading) or ADDED_TO_STAGE
for your loaded movie and implement your functionality this way.
精彩评论