You will get this error “ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller” when you call “unload()” method from Loader object if you have added/reparented its content like the code below:
1 2 3 4 5 6 7 8 9 10 11 12 | var myLoader:Loader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); function onCompleteHandler(e:Event):void { // Add loaded content to the display list someMC.addChild(e.target.content); // Call unload now, this will cause the error myLoader.unload(); } myLoader.load(new URLRequest("example.swf")); |
If you add a child object that already has a different display object container as a parent, the object is removed from the child list of the other display object container. That is why you get this error…, but not a problem if you only load the item once and have no requirement to unload.
To avoid the error and unload the item, you can either:
1. Add the Loader object to the display list instead then remove it by “unload()” method.
1 2 3 4 5 6 7 8 9 10 11 12 | var myLoader:Loader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); function onCompleteHandler(e:Event):void { // Add loader to the display list instead someMC.addChild(e.target); // This will not cause the error myLoader.unload(); } myLoader.load (new URLRequest("example.swf")); |
Basically “unload()” method removes a child of the Loader object and the associated LoaderInfo object is reset to null. Also the child is not necessarily destroyed because other objects might have references to it…
2. Or if you have re-parented the loaded content, remove the content from the current parent display object by “removeChild()” method then set any references to loader and content to null.
1 2 3 4 5 6 7 8 9 10 11 12 13 | var myLoader:Loader = new Loader(); myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler); function onCompleteHandler(e:Event):void { // Add loaded content to the display list someMC.addChild(e.target.content); // Remove the content someMC.removeChild(e.target.content); myLoader = null; } myLoader.load(new URLRequest("example.swf")); |
It is similar to “unload()” method, i.e – remove child and nullify references.
9 users commented in " AS3 Loader unload() error "
Follow-up comment rss or Leave a TrackbackI used the second method. However, when I want to recall the loader I had the following error:
Error #1009: Cannot access a property or method of a null object reference.
I used:
ldrImage = new Loader();
instead of:
myLoader = null;
and it workd!!
(sorry: I named the loader “ldrImage” instead of “myLoader”)
If you want to re-use myLoader, you will always need to instantiate it since we have set it to null to avoid errors when unloading.
i set it to null, but there are still listeners listening from the unloaded swf…
Hey,
I had the same #2505 error. I did what Ed did,
myloader = new Loader();
myloader.load(newValue);
And it worked!
But doesn’t this mean that each previously loaded content stays? (Which isn’t a problem for me, because there are only 9 images loaded to my movie) – Wouldn’t it use up more memory, because it’s creating a new instance of a loader?
** confused **
Hey there!
First of all thanks for the info in this article, it helped me a lot. I’ll post my implementation, maybe it will help some of you.
var mLoader:Loader = new Loader();
function startLoad(urlParam:String)
{
if(mcHolder.numChildren > 0)
{
mcHolder.removeChildAt(0);
}
if(urlParam != “”)
{
mLoader = new Loader();
mLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
mLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onProgressHandler);
mLoader.load (new URLRequest(urlParam));
}
else
{
trace(“We have an empty string URL param.”);
}
}
function onCompleteHandler(e:Event):void
{
mcHolder.addChild(e.target.content);
}
function onProgressHandler(mProgress:ProgressEvent)
{
var percent:Number = mProgress.bytesLoaded/mProgress.bytesTotal;
trace(percent);
}
“mcHolder” is an emtpy MC on the stage. You can call the function multiple times with:
startLoad(“movie.swf”);
or startLoad(“”);
new here, first post for help.
i have a simple slide show type .fla (AS 3.0)
slide1, space to goto slide2, space to goto slide3, etc. then, i need to load an external .swf to act as slide4. it loads, plays, and then stops on the last frame of the external .swf. I cannot find info on how to wait for the external swf to play and finish, and then jump right into the main movie at slide5.
this make sense?
any ideas?
Hi, I’m trying to understand this, but my problem is a bit different so I was wondering if you could help me sort it out. I’m creating a test that clears the page and then loads new XML content and I’m getting this very same error. Here is my code:
public function LoadXML() {
XML_URL = “exam-problems” + pageNumber + “.xml”;
contentXMLURL = new URLRequest(XML_URL);
contentXMLLoader = new URLLoader(contentXMLURL);
contentXMLLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCompleteHandler);
contentXMLLoader.addEventListener(Event.COMPLETE, xmlLoaded);
function xmlLoaded(event:Event):void {
var theXML:DisplayObject = event.target.content;
if (contentXMLLoader != null) {
unload();
}
contentXML = new XML(event.target.data);
for (var i:int = 0; i<contentXML.*.length(); i++) {
questions[i] = contentXML.PROBLEM[i].@QUESTION;
choicesArry1[i] = contentXML.PROBLEM[i].@CHOICE1;
choicesArry2[i] = contentXML.PROBLEM[i].@CHOICE2;
choicesArry3[i] = contentXML.PROBLEM[i].@CHOICE3;
choicesArry4[i] = contentXML.PROBLEM[i].@CHOICE4;
answers[i] = contentXML.PROBLEM[i].@THEANSWER;
}
numberOfProblems = questions.length;
startTestCreation();
}
}
function onCompleteHandler(e:Event):void {
// This will not cause the error
contentXMLLoader.unload();
contentXMLLoader = null;
}
Can someone help me figure out what I'm doing wrong? Thanks.
[...] [...]
Leave A Reply