So I have a main swf that loads a child swf using the following code:
import flash.display.Loader;
import flash.net.URLRequest;
import flash.events.KeyboardEvent;
import flash.events.Event;
import flash.display.MovieClip;
import flash.events.IEventDispatcher;
import flash.events.ProgressEvent;
var swfLoader:Loader;
var swfContent:MovieClip;
var loaded:Boolean=false;
stage.addEventListener(KeyboardEvent.KEY_UP, loadSWF);
function loadSWF(e:KeyboardEvent):void {
if (e.keyCode!=32) return;
if (!loaded) {
var req:URLRequest=new URLRequest();
req.url="test/LevelTest.swf";
swfLoader=new Loader();
setUpListeners(swfLoader.contentLoaderInfo);
swfLoader.load(req);
} else {
unloadSWF();
}
}
function setUpListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(Event.COMPLETE, addSWF);
dispatcher.addEventListener(ProgressEvent.PROGRESS, preloadSWF);
}
function preloadSWF(e:ProgressEvent):void {
var perc:int=(e.bytesLoaded/e.bytesTotal)*100;
}
function addSWF(e:Event):void {
e.target.removeEventListener(Event.COMPLETE, addSWF);
e.target.removeEventListener(ProgressEvent.PROGRESS, preloadSWF);
swfContent=e.target.content;
loaded=true;
addChild(swfContent);
}
function unloadSWF():void {
loaded=false;
swfLoader.unloadAndStop();
removeChild(swfContent);
swfContent=null;
}
The idea is that the child swf is loaded upon pressing the spacebar, and then unloaded when you press it again. Every time I reload the swf, the frames per second drops significantly. I was under the impression that unloadAndStop() removes EVERYTHING, even the listeners. But this does not seem to be the case. What is the easiest way to completely remove everything from the child swf so it can be garbage collected? Do I just have write a destroy function for everything?