History

The Loader.unloadAndStop feature was added to Action Script 3's API to automate a process that was previously manual when using Loader.unload.

Though Loader.unload removes the child of the Loader object, the unloaded object will still run in the background until it is actually disposed of by the garbage collector. Since the garbage collector does not dispose of objects that are referred to, the "unloaded" content could perhaps never be removed (for example, event listeners could reference the object).

Example: A SWF file that has a music track is loaded into an application. Later, the SWF is unloaded using Loader.unload(). Though the SWF will be removed from the screen, the music will still be heard.

SOLUTION

Loader.unloadAndStop is a new addition to Action Script 3's API. It helps developers to correctly stop and unload loaded content using the Loader.load/loadBytes APIs.

Loader.unloadAndStop is very similar to Loader.unload. However, it adds one more step. After the "unload" event is dispatched to the LoaderInfo class, Flash Player recursively attempts to stop and clear as many objects as possible (Sounds, NetStreams, EventListeners, etc.) within the loaded SWF. This feature is especially useful when unloading unknown 3rd party content.

Example

import flash.display.Loader
		
	    import flash.net.URLRequest
		
	    import flash.events.Event
		
	  
		
	    var myLoader:Loader = new Loader();
		
	    myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,function(e:Event){
		
	    addChild(myLoader);
		
	    });
		
	  
		
	    myLoader.load(new URLRequest("item2.swf"));
		
	    btnUnload.addEventListener(MouseEvent.CLICK,function(){
		
	    // Calling this method will unload and recursively stop the content of the movie.
		
	    myLoader.unloadAndStop();
		
	    }); 

Keywords: kb403670

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy