Bookmark and Share

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:

?View Code ACTIONSCRIPT
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.

?View Code ACTIONSCRIPT
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.

?View Code ACTIONSCRIPT
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.