Visit SlideShowPro.net  Community Forums
SlideShowPro Support Wiki
  
Flash component

How to: Unload SWF in ActionScript 3

One important way that the ActionScript 3 language is different than ActionScript 2 is how it handles the removal of events, audio, and other processes that are transpiring in a SWF when unloaded from another SWF. In ActionScript 2, you could simply remove the parent MovieClip containing an externally loaded SWF and everything happening inside the unloaded SWF would stop. With ActionScript 3, processes (like audio) are handled as separate streams (so to speak) and can sometimes linger on and continue playback even after the external SWF has been unloaded.

If you're developing Flash content and have run into this, there are a couple of options which we'll detail here.

Option one: Use unloadAndStop()

Added by Adobe to Flash Player 10 is a new method for the Loader class named unloadAndStop(). It enhances the existing unload() method by attempting to unload child SWF contents and stopping the execution of commands and events contained therein. For example, the code below loads an external SWF then uses unloadAndStop() to remove it after clicking a button:

var ldr:Loader = new Loader();
var url:String = "child.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
addChild(ldr);

// unload_btn is on the stage
unload_btn.addEventListener("click",afterClick);

function afterClick(e:MouseEvent):void {
    ldr.unloadAndStop();
}

Important note: unloadAndStop() is only available in Flash Player 10, which means it will only function for users who have the Flash Player 10 plugin installed. If this is an issue, see the next option.

Option two: Calling halt()

Before Adobe introduced unloadAndStop() we built into the SlideShowPro Player its own cleanup method named halt(). This method behaves similarly, except it must be called on the component instance prior to the SWF being unloaded. Here's a revised example of the above that uses this option:

var ldr:Loader = new Loader();
var url:String = "child.swf";
var urlReq:URLRequest = new URLRequest(url);
ldr.load(urlReq);
addChild(ldr);

// unload_btn is on the stage
unload_btn.addEventListener("click",afterClick);

function afterClick(e:MouseEvent):void {
	var loadClip:MovieClip = MovieClip(ldr.content);
	loadClip.haltSSP();
    ldr.unload();
}

The second part of this equation is the haltSSP() method shown above. This must be included in the timeline of the child SWF, like so:

function haltSSP() {
	my_ssp.halt();
}

So when the button is clicked in this scenario, the haltSSP() method inside the child SWF is called, the SlideShowPro Player shuts down whatever is currently going on, then ldr.unload() is called which removes the child SWF from the display list of the parent movie.

Summary

Calling unloadAndStop() instead of using the SlideShowPro Player's halt() is a cleaner, simpler approach to solving this issue. It does however require publishing a SWF to Flash Player 10, so keep that in mind when publishing slideshows.

Page last modified by tdominey on November 14, 2010, at 05:49 PM
© 2011 SlideShowPro. All Rights Reserved.