当外部加载swf时,removechild不工作

问题描述 投票:0回答:1

我的问题是放在swf中的关闭按钮(removechild)完全独立工作但是当swf从另一个加载时,该按钮不再起作用。

这是外部swf代码:

eti_scroll.scrollTarget = box_eti ;
hab_scroll.scrollTarget = box_hab ;
com_scroll.scrollTarget = box_com ;
descr_scroll.scrollTarget = box_descr ;



       exit.addEventListener(MouseEvent.CLICK, exitBtn_clickHandler);  

    function exitBtn_clickHandler(event:MouseEvent):void {  
    if(this.parent) this.parent.removeChild(this);
}  

这是来自主swf的按钮代码:

menu_button_2.addEventListener(MouseEvent.CLICK, fl_MouseClickHandler2);

function fl_MouseClickHandler2(event:MouseEvent):void
{
    var myLoader:Loader = new Loader();                     
    var url:URLRequest = new URLRequest("pages/page_template.swf");  
    myLoader.load(url);                                  
    addChild(myLoader);
}

在主swf中没有导入包或loaderclass

以下是fla版本和示例版本的链接:http://www.mediafire.com/file/5dzqnq3kth6n6dt/examples.rar

当我使用@organis的跟踪代码并从外部SWF文件加载时抛出错误:抛出的错误:

 I am here! Exit Button: [object SimpleButton] object Event Handler: function
 Function() {} function Exit.addEventListener: function Function() {} function 
MouseEvent.CLICK This: [object MainTimeline] object Parent: [object Loader] 
object Parent.removeChild: function Function() {} function Error: Error #2069:
 La clase Loader no implementa este método(The Loader Class doesn't implement 
this method). at Error$/throwError() at flash.display::Loader/removeChild() at 
page_template_fla::MainTimeline/onClick()[page_template_fla.‌​MainTimeline::frame1‌​
:65] –
actionscript-3 flash removechild
1个回答
1
投票

让我解释一下如何处理这类问题。一旦应该工作的东西没有,首先要做的是确定问题的确切位置,这样你就可以诊断问题是什么,而不是模糊地理解某个地方存在问题。

所以你从独立运行中读取所有跟踪,然后从加载运行中读取所有跟踪,肯定必须有区别。在找到它之后,你会考虑到这种差异。

// If this does not work, that means scripts do not work in the loaded SWF at all.
trace("I am here!");

// If this doesn't work the same as standalone, that means
// something breaks while constructing the loaded content.
trace("Exit Button:", exit, typeof(exit));

// Lets check id the event handler is doing fine.
trace("Event Handler:", onClick, typeof(onClick));

// If the method is not present on the object,
// something is deeply wrong with the whole thing.
trace("Exit.addEventListener:", exit.addEventListener, typeof(exit.addEventListener));

exit.addEventListener(MouseEvent.CLICK, onClick);  

function onClick(event:MouseEvent):void
{
    // If it doesn't work that means there's no mouse event.
    trace("MouseEvent.CLICK");

    // Just to check things out.
    trace("This:", this, typeof(this));
    trace("Parent:", this.parent, typeof(this.parent));
    trace("Parent.removeChild:", this.parent.removeChild, typeof(this.parent.removeChild));

    if(this.parent) this.parent.removeChild(this);
}

UPD:现在,你得到了错误(你应该首先在你的问题中提到它,实际上)

错误:错误#2069:La clase Loader no implementaestemétodo(_ Loader Class不实现tihs method_)。 at Error $ / throwError()at flash.display :: Loader / removeChild()at page_template_fla :: MainTimeline / onClick()[page_template_fla.MainTimeline :: frame1:65]

现在看看这里(我喜欢这个链接,因为它解释了很多):https://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e26.html

您的脚本位于主时间轴上。因此,当您在独立模式下运行时,this.parent指向可以添加和删除子项的阶段,没问题。

然后,当您在加载模式下运行它时,层次结构将进入主SWF阶段 - >主SWF根目录 - > ... - > Loader - >加载SWF根目录。正如您所看到的,当您解决this.parent时,您将获得Loader实例,即您在主SWF中使用的实例,以加载另一个实例。 Loader是一个DisplayObjectContainer,但不用于添加/删除子项,因此它会抛出上面提到的异常。

有了这一切,我建议另一种方法去除内容:

exit.addEventListener(MouseEvent.CLICK, onClick);  

function onClick(event:MouseEvent):void
{
    // Unsubscribe to help Garbage Collector do its job.
    exit.removeEventListener(MouseEvent.CLICK, onClick);  

    // Hide the content.
    visible = false;

    // Remove all of its children.
    removeChildren();
}

或者您可以查看脚本运行的情况并采取相应措施:

exit.addEventListener(MouseEvent.CLICK, onClick);  

function onClick(event:MouseEvent):void
{
    // Unsubscribe to help Garbage Collector do its job.
    exit.removeEventListener(MouseEvent.CLICK, onClick);  

    // Checking for
    // if (parent == null)
    // is unnecessary here because if that was the case
    // there won't be a mouse event in the first place.

    var aParent:DisplayObjectContainer = parent;

    if (aParent is Loader)
    {
        // Loaded case.
        aParent.parent.removeChild(aParent);
    }
    else
    {
        // Standalone case.
        parent.removeChild(this);
    }
}

但是,这种计算只能在简单的情况下使用,而不会在更复杂的设置(跨域内容,安全沙箱等)中使用。

© www.soinside.com 2019 - 2024. All rights reserved.