Getting the Classes from external SWFs

When it comes to divide a flash application into separate swfs, its a whole new ball-game in AS3 than in AS2. In this particular case I think AS2 was rocking and simple. But then its as simple in AS3 as in AS2,one just need to change the thought process. First of all one must understand that whatever is happening in AS3 is an Object-Event process. Then everything seems clearer. So lets start the code part.

I have two SWF files, one is having audio library and one is my application FLA.In my audio Library FLA, I have two audio files in the library,with Class name as “saumya.audio.One” and saumya.audio.Two”. These two files are set to “Export for Actionscript” and “Export in Frame 1”. So this library FLA does not contain anything else except two “mp3” in library with the above settings. Now lets publish the FLA to produce a SWF file. Let the file named “audioPackaging.swf”. We have the audio library in place. Now all that remains is access these classes from my main container or application SWF. There is another FLA, which is supposed to be the base container or the main application FLA. This file is supposed to load the audio library into it and play them. The process of loading is same as loading any other SWF,using “Loader” class.

Once”audioPackaging.swf” is loaded into the application SWF, we will get the “LoaderInfo” object from the complete event object. This “LoaderInfo” object is the key to get all the classes in the loaded SWF. We will go into detailed descrition once we see the code. Below is the code to load the SWF and then access the “LoaderInfo” object.

var l:Loader=new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE,onSongs);
l.load(new URLRequest(“audioPackaging.swf”));
function onSongs(e:Event):void{
trace(e.target);
var lf:LoaderInfo=LoaderInfo(e.target);
}

All the classes in an externally loaded SWF is loacated inside an object of “ApplicationDomain” class. This class is in the “flash.system.ApplicationDomain” package. Do not worry if the words seem alien to you. These are actually our new friends in AS3 :) . Now the good news is to find our friends we do not have to search anywhere! There is a property in “LoaderInfo” object which contains our friend “ApplicationDomain” and the property is called “applicationDomain”. Phew,there we go. The revised code now look like

import flash.system.ApplicationDomain;

var l:Loader=new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE,onSongs);
l.load(new URLRequest(“audioPackaging.swf”));
function onSongs(e:Event):void{
trace(e.target);
var lf:LoaderInfo=LoaderInfo(e.target);
var Two:Class=    (lf.applicationDomain.getDefinition(‘saumya.audio.Two’))as Class;
var t:Sound=new Two();
t.play();
}

Now you can see we have a method in “ApplicationDomain” class which actually returns the Class definition object. If you still remember we have already assigned the class name to the second audio file in the properties pannel as “saumya.audio.Two”. Once we get the reference to “ApplicationDomain” object, its a matter of calling the method “getDefinition(‘classDefinition’)” of this object and casting it as a Class. Thats all.We will have a proper Class definition now onwards. since we know that “saumya.audio.Two” is a “Sound” class, we can go ahead and play it like any other “Sound” class.

I had put this example for “Sound” as audio was and still is a painful part in flash application development. But the concept can be applied to any class definitions you may want to load dynamically or want to share as a separate SWF.

Happy Flashing :)

Advertisements

6 thoughts on “Getting the Classes from external SWFs

  1. How would you remove the class definition from memory? Supose you have 1000 swfs, each with its own definitions. Each time you load one, the definitions from the loaded swf will always remain in memory, even if you unload said swf, which would eventually lead to a massive memory leak.

  2. Cheers Saumya, that code got me out of a tight spot!
    p.s. I found this through google, but I actually know who you are already through the Sparrow forum funnily enough!

    take it easy mate,

    Alex H

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s