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
Tags: actionscript 3, external library, Flash, loading sound from swf, swf
July 29, 2009 at 11:26 am |
Very clear explanation, thanks a lot
September 22, 2009 at 10:41 pm |
Thanks, very helpful information.