简体   繁体   English

在同一按钮中卸载SWF文件,该按钮可以加载外部文件以最大程度地减少加载时间

[英]Unload SWF File in a Same Button that Load the External File to Minimize Loading Time

I have two SWF files. 我有两个SWF文件。 First one is home.swf and the second one is menu.swf . 第一个是home.swf ,第二个是menu.swf Each SWF file has a button that load external SWF file. 每个SWF文件都有一个加载外部SWF文件的按钮。

This is the code inside home.swf file: 这是home.swf文件中的代码:

stop();
menu_btn.addEventListener(MouseEvent.CLICK, func_menu);
function func_menu(e:MouseEvent):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("menu.swf"));
}

function onComplete(e:Event):void {
    var movie:* = LoaderInfo(e.currentTarget).content;
    stage.addChild(movie);
}

This is the code inside menu.swf file: 这是menu.swf文件中的代码:

stop();
home_btn.addEventListener(MouseEvent.CLICK, func_home);
function func_home(e:MouseEvent):void {
    var loader:Loader = new Loader();
    loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
    loader.load(new URLRequest("home.swf"));
}

function onComplete(e:Event):void {
    var movie:* = LoaderInfo(e.currentTarget).content;
    stage.addChild(movie);
}

If I click the buttons numerous times, the SWF files gets longer time to load. 如果我多次单击按钮,则SWF文件的加载时间会更长。 I notice that those code doesn't unload files. 我注意到这些代码不会卸载文件。 What code do I need to unload the previous SWF files? 卸载以前的SWF文件需要什么代码? I'm new in actionscript. 我是动作脚本新手。 Please go easy on me. 请对我好一点。

I understand from the question that it's highly likely that you'll switch between the SWFs often (loading home and unloading menu, and vice versa). 从这个问题中我了解到,您很有可能会经常在SWF之间进行切换(加载主页和卸载菜单,反之亦然)。

I suggest you create a container SWF, which will load both SWFs, and save references to the loaders / their content on load complete. 我建议您创建一个容器SWF,该容器将同时加载两个SWF,并在加载完成时保存对加载器的引用/它们的内容。

Once both are loaded, simply call addChild() and removeChild() to swap them. 两者都加载后,只需调用addChild()和removeChild()即可交换它们。

In case you want to trigger the swap from within the SWFs, you can dispatch an event when clicking on the buttons created in them, and listen to dispatched events in the container SWF. 如果要从SWF内部触发交换,则可以在单击SWF内创建的按钮时调度事件,并监听容器SWF中的调度事件。

That way you can swap them as many times as you'd like in an instant without having to reload them every time, plus you avoid constantly allocating and deallocating memory. 这样,您可以立即交换它们多次,而不必每次都重新加载它们,而且还避免了不断分配和释放内存的情况。

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

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