Tuesday, September 11, 2007
as3: a *generic* COMPLETE listener for Loader
Tuesday, September 11, 2007
Today I was banging my head up against the notion of loading multiple things and using a single COMPLETE event to serve them all. My head was banging against getting the actual instance of who was being loaded into for the event. I didn't see this covered anywhere online or in the documentation, but I imagine that TONS of AS3 developers would prefer this approach (opposed to setting up multiple COMPLETE events for different loaders).
I was checking event.target (which gives you the LoaderInfo object, but I wanted to get the actual Loader object & the .name I assigned it). In this way I could assign variables to the content of the clips I was loading into easily. It took a while and another pair of eyes (thanks Andrew O.), and we imported the LoaderInfo object, did a little casting, and using the .name property, are able to easily reference loaded stuff (say code/elements in SWFs) in a document class. This is the basic code showing whats going on.
In two different SWFs that get loaded into the AS3 document class are simple variables (just to test that the document class can get in there): var iam:String = "viewX.swf";. Each SWF has their own file name populating the variable. Now for the important bits in the document class (typing this by hand as an example... the indention is probably off):
I thought some might benefit from seeing this because I didn't see an example of it online. Perhaps this is super-common knowledge for those already traversing the waters of AS3, but it wasn't for me. It was time well spent though. Hope someone can learn from this to make their lives easier too.
I was checking event.target (which gives you the LoaderInfo object, but I wanted to get the actual Loader object & the .name I assigned it). In this way I could assign variables to the content of the clips I was loading into easily. It took a while and another pair of eyes (thanks Andrew O.), and we imported the LoaderInfo object, did a little casting, and using the .name property, are able to easily reference loaded stuff (say code/elements in SWFs) in a document class. This is the basic code showing whats going on.
In two different SWFs that get loaded into the AS3 document class are simple variables (just to test that the document class can get in there): var iam:String = "viewX.swf";. Each SWF has their own file name populating the variable. Now for the important bits in the document class (typing this by hand as an example... the indention is probably off):
package net.ericd.baseFor me, this is going to save me TONS of time and coding to have a single COMPLETE function work for me in this kind of context. I was looking for something a little more like MovieClipLoader but we don't have that in AS3 which is perfectly fine with me. Just a different hurdles to navigate here.
{
import flash.display.DisplayObject;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.*
import flash.display.Stage;
import flash.net.URLLoader;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.LoaderInfo;
public class Application extends Sprite
{
private var FOO:Object;
private var BAR:Object;
private function loadViewElements():void
{
var loader1:Loader = new Loader();
var loader2:Loader = new Loader();
loader1.name = "foo";
loader2.name = "bar";
var mc1_URL:URLRequest = new URLRequest( "views/view.swf" );
var mc2_URL:URLRequest = new URLRequest( "views/view2.swf" );
loader1.load( mc1_URL );
loader2.load( mc2_URL );
loader1.contentLoaderInfo.addEventListener(
Event.COMPLETE, onClipLoaded );
loader2.contentLoaderInfo.addEventListener(
Event.COMPLETE, onClipLoaded );
var foo_holder:MovieClip = new MovieClip();
foo_holder.addChild( loader1 );
addChild( foo_holder );
var bar_holder:MovieClip = new MovieClip();
bar_holder.addChild( loader2 );
addChild( bar_holder );
}
private function onClipLoaded( event:Event ):void
{
// Need to get Loader from LoaderInfo!!
trace( event.target ); //[object LoaderInfo]
var loaderInfo:LoaderInfo = event.target as LoaderInfo;
// Important code below:
trace( loaderInfo.loader ); // [object Loader]
var myName:String = loaderInfo.loader.name;
// Now we can do this:
if( myName == "foo" )
{
// We can reference the cont
FOO = event.target.content;
trace( FOO.iam ); // view.swf
} else if( myName == "bar" )
{
BAR = event.target.content;
trace( BAR.iam ); // view2.swf
}
}
}
}
I thought some might benefit from seeing this because I didn't see an example of it online. Perhaps this is super-common knowledge for those already traversing the waters of AS3, but it wasn't for me. It was time well spent though. Hope someone can learn from this to make their lives easier too.
Comments:
There are currently 8 Comments:
-
Leif said...“Eric,
Sorry to bug you in the comments. Did you create the MAX badge that you have in the right column (the one that counts down for Chicago)?
If so, may I have a copy of the swf for my user group sites?
If not, where did you get it?
leif [dot] wells [at] augatlanta [dot] org”



