【问题标题】:Unloading swfs loaded with Loader::load()卸载用 Loader::load() 加载的 swfs
【发布时间】:2010-01-23 21:39:31
【问题描述】:

我正在使用Loader::load() 成功地将 swf 加载到我的主 swf 中,然后我将它们添加为 Sprite 的子项。当发生其他事件时,我想根据需要删除 swf。我看过unload() 和removeChildAt() 都没有成功。

我只添加了addChild() 调用来尝试固定加载的实例,以便我可以删除它。没有addChild();

,加载工作正常

我也尝试过发布到播放器 v.10 并使用myLoader.unloadAndStop();但这也没有效果;

以下演示代码显示了我的问题。我看到添加了一个孩子并删除了一个孩子,但 intro.swf 仍在播放。

import flash.display.Loader; 
import flash.display.Sprite;
import flash.display.LoaderInfo;
import flash.net.URLRequest;

var myLoader:Loader = new Loader();
var holderMC:Sprite = new Sprite();
var myRequest:URLRequest = new URLRequest('intro.swf');

myLoader.load(myRequest);
holderMC.addChild(myLoader);
trace("initial:"+holderMC.numChildren);  // traces initial:1

while(holderMC.numChildren > 0) {
 holderMC.removeChildAt(0);
 trace("now there are:"+holderMC.numChildren); // traces now there are :0
}
myLoader.unload();

// 编辑- 也试过了:

myLoader.unloadAndStop();
myLoader = null;

有什么想法吗?

【问题讨论】:

    标签: actionscript-3 flash loader


    【解决方案1】:

    你肯定有其他事情发生在这里。首先,您如何看待“intro.swf”?您正在创建 holderMC,并将加载的 swf 添加为子项,但是您何时将 holderMC 添加到显示列表中?

    从视图中删除电影的正确方法是:

    holderMC.removeChild(myLoader);
    

    为了让 holderMC 的内容被标记为垃圾回收,您需要将其设置为 null。所以,

    holderMC.removeChild(myLoader);
    myLoader.unload(); // this will set the content (the movie itself) to null.
    myLoader = null; // npw the loader can be garbage collected too
    

    如果您正在执行 removeChild 并且它仍然显示,那么您将需要发布更多代码以显示问题的真正所在。

    【讨论】:

    • 感谢您的回答!我认为问题在于我正在将旧的 AS2 文件转换为 AS3,当我开始使用新文件并使用您的建议时,问题得到了解决。谢谢!
    猜你喜欢
    • 2017-07-04
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 2014-04-30
    • 2011-01-27
    • 1970-01-01
    • 2018-12-25
    相关资源
    最近更新 更多