Skip to main content
Known Participant
March 19, 2010
Answered

Loading .swf, addChild, and removeChild

  • March 19, 2010
  • 2 replies
  • 1378 views

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!

This topic has been closed for replies.
Correct answer Ned Murphy

Part of the problem is your trying to remove the loader just after you define it...  it is not a child of anything at that point.  Try the following instead of what you have.   Another problem you can avoid is to not nest functions...

var loader:Loader
var loaderState:Boolean = false;

function newPage():void {
if (loaderState){
        top_mc.removeChild(loader);
    }

    var req:URLRequest=new URLRequest(""+target+""+".swf");
    loader = new 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 {
    top_mc.addChild(loader);
    loaderState=true;
}

2 replies

Inspiring
March 19, 2010

Hi,

In addition to previous comment...

You can refer to the child object with top_mc.getChildAt(0) where 0 is its reference in the array of child objects or refer to top_mc.getChildByName("theName") if you have previously assigned a name to it.

Hope it helps.

Ned Murphy
Ned MurphyCorrect answer
Legend
March 19, 2010

Part of the problem is your trying to remove the loader just after you define it...  it is not a child of anything at that point.  Try the following instead of what you have.   Another problem you can avoid is to not nest functions...

var loader:Loader
var loaderState:Boolean = false;

function newPage():void {
if (loaderState){
        top_mc.removeChild(loader);
    }

    var req:URLRequest=new URLRequest(""+target+""+".swf");
    loader = new 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 {
    top_mc.addChild(loader);
    loaderState=true;
}

libenganAuthor
Known Participant
March 19, 2010

Alright, now I see what was happening.  That worked beautifully. Thanks

Also, thanks for the advice on functions; definitely makes things cleaner!

Ned Murphy
Legend
March 20, 2010

You're welcome