Loading .swf, addChild, and removeChild
Hello all,
I'm trying load an swf when you click a button, and load another in it's place when you click a different button. I'm using addChild to insert the swf onto the stage. Problem is that every time I click a new button, the new swf is loaded on top of the previous one, and I need the previous one to disappear. I tried using the removeChild method before the new swf is loaded to no effect.
Here's the function I'm using for the loader with my removeChild attempt in bold:
function newPage():void {
trace("load activated");
trace(""+target+".swf");
var req:URLRequest=new URLRequest(""+target+""+".swf");
var loader:Loader = new Loader();
if (loaderState==true){
top_mc.removeChild(loader);
}
loader.load(req);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, preLoad);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, fileLoaded);
function preLoad(event:ProgressEvent):void {
var percent:Number=Math.round(event.bytesLoaded/event.bytesTotal*100);
top_mc.preload_txt.text=String(""+percent+"");
}
function fileLoaded(event:Event):void {
trace("file loaded");
top_mc.addChild(loader);
loaderState=true;
}
}
The error I get is as follows:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at main_fla::MainTimeline/newPage()
at main_fla::MainTimeline/workLoaderEnter()
at main_fla::MainTimeline/clickF()
Perhaps there is a way to give the addChild an instance name so that I can remove it later?
Any ideas?
Thanks!