简体   繁体   English

AS3-如何从嵌套的动态加载的动画片段的根中调用函数?

[英]AS3 - How to call a function in the root from a nested - dynamically loaded movieclip?

Does anyone know the best method to trigger a function 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? 有谁知道使用AS3从动态加载的动画片段(使用addchild加载)在根中触发功能的最佳方法,我知道以根为目标不是这样做的最佳方法吗?

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. DanielB有一点,这并不是最好的设计...加载电影时可以设置调用函数的对象。 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? 您加载的SWF应该从加载的电影中调用一个函数才能正常工作吗? so the loaded movie will never run standalone? 这样加载的电影将永远不会独立运行?

You should implement all functionality in your loader (parent) swf. 您应该在加载程序(父级)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. 您可以对已加载的电影执行COMPLETE (加载)或ADDED_TO_STAGE ,并以这种方式实现功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM