0

I have two SWF files. First one is home.swf and the second one is menu.swf. Each SWF file has a button that load external SWF file.

This is the code inside home.swf file:

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:

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. I notice that those code doesn't unload files. What code do I need to unload the previous SWF files? I'm new in actionscript. Please go easy on me.

1
  • You load the same "swf" file multi times which consume the memory of the device . I think you need to load both "swf" files in the parent with one loader event listener , so you can use "loader.unloadAndStop();" before loading the new swf file. Commented Dec 26, 2015 at 10:23

1 Answer 1

0

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).

I suggest you create a container SWF, which will load both SWFs, and save references to the loaders / their content on load complete.

Once both are loaded, simply call addChild() and removeChild() to swap them.

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.

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.