Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.


Flash's LoaderInfo Reuse in ActionScript 3.0

Posted February 02, 2009 by senocular

Here's an interesting factoid/precaution for you ActionScript developers out there: Did you know that Loader instances use the same LoaderInfo for all loaded content?

Why is this important? Most of the time, it's not. After all, you probably didn't already know this, cared to think about it, or even had any problems because of it. This is because, typically, when you load something in a Loader and then unload it or replace it with something else, you no longer need or interact with the original content you loaded. But what if that content is still used? Then, you might find yourself in for a surprise since Loader.contentLoaderInfo is the same LoaderInfo object each DisplayObject loaded into that Loader uses for it's own loaderInfo despite the originating SWF.
Lets take a closer look at what this "reuse" of LoaderInfo instances really means. Consider the following code run in a SWF stored along with 3 other SWFs named ChildA-C.
var myContent:Array = ["ChildA.swf", "ChildB.swf", "ChildC.swf"];
var myLoader:Loader = new Loader();

function loadNext():void {
if (myContent.length) myLoader.load(new URLRequest(myContent.shift()));
var i:int = numChildren;
while(i--) trace("Child at "+i+": "+getChildAt(i).loaderInfo.url);

function loadCompleteHandler(event:Event):void {
trace("Loaded: "+myLoader.contentLoaderInfo.url);

myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadCompleteHandler);

This code uses a single Loader to load 3 different SWFs and then transfer the content of those SWFs into its own timeline. Each time a SWF is loaded, the url property from it's LoaderInfo is traced. Additionally, when all the SWFs have been loaded, the process is repeated for each (now children of the current timeline).

This is a sample of what the results might look like:
Loaded: file:///C|/ChildA.swf
Loaded: file:///C|/ChildB.swf
Loaded: file:///C|/ChildC.swf
Child at 2: file:///C|/ChildC.swf
Child at 1: file:///C|/ChildC.swf
Child at 0: file:///C|/ChildC.swf

You can see that despite each url for each SWF, when loaded, was correct, once they were all traced out in the end, they all shared the same value - that of the last SWF loaded. This is because each of those SWFs reference the LoaderInfo provided to them by the Loader that loaded them. Once a new SWF is loaded into that loader, the data for that LoaderInfo changes to match the new content despite the fact that older, unrelated content may still refer to the same object.

So, as a best practice, it may be a good idea to avoid reusing Loaders for any content which will be removed from that Loader for use in another context. If, however, the content is unloaded before new content is loaded in, you may be in the clear. But this is something to especially pay attention to if for any reason you are relying on data provided by LoaderInfo instances.