Access / load library assets from another SWF in Flash CS3
In this short article I would like to focus on a simple technique to load library assets from another SWF which is actually used as class container. I already used that technique several times and it comes in handy in some situations. In my opinion it is especially handy when you are working on a project with several people and one is doing the design of the assets to be used. It also can be handy when it comes to streamlined preloading of several assets. We use one SWF then as dataContainer for all the assets, the base swf loads the dataSWF and from then on the assets are accessible and can be instantiated.
Let’s illustrate the technique using a simple example:
Imagine we have one simple SWF (eg : data.swf) that contains one library item: a movieclip with a designed little square with a class name Square that uses Movieclip as his base class. As you know, in Flash CS3 a class is automatically generated when you not specify a custom class so in our SWF we can normally instantiate a Square object by writing this code:
var mySquare:Square = new Square();
But imagine that we have another SWF (eg: base.swf) where we would like to instantiate also Square objects. Thats were the technique of dynamic class loading comes in. First thing we have to do is to load the “data.swf” inside the base.swf like this:
var myLoader:Loader = new Loader();
var myUrlReq:URLRequest = new URLRequest(”data.swf”);
myLoader.load(myUrlReq);
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoaded);
As you can see we call the onLoaded function when the data.swf is fully loaded. The onLoaded function is the place where we are going to access the SWF his contents, in our case the Square in the library. The final onLoaded function looks like this:
function onLoaded(event:Event):void
{var Square:Class = event.target.applicationDomain.getDefinition(”Square”) as Class
var mySquare:MovieClip = new Square() as MovieClip;
addChild(mySquare);
}
As you see we first instantiate a class object and we acces the Square class (inside the library of the data.swf) and cast it as a Class also. From then on you can instantiate as much squares you want. To avoid flash saying that a square datatype isn’t found we cast a new Square object immediately to a MovieClip where it is extending from. The last thing we do is adding the square to the displayList by calling the addChild method.
Very simple technique but comes in handy in some situations ![]()
Filed under: Flash CS3




















Really interesting… thanks for the tip.
[...] “Load library assets from another SWF which is actually used as class container. [snip] Especially ha…” - Sweet! Spread the word! [...]
thank you very much!!!
was looking for that all day.
used to get same custom event class from loaded movie.
I’m having difficulty with this technique. I have several “skin” swfs. Each swf contains a CustomButton symbol, and each swf’s version of the button graphic is different. I can load swf1, create a new CustomButton() and add it to the stage to see it render. I then remove and destroy the custom button instance, then unload swf1. Finally, I load swf2, create new CustomButton() and add to the stage. The first swf’s butt graphic is drawn!! I’ve been able to “solve” the problem by wrapping each Flash generated swfs library items in a Flex generated swf using a class that @Embeds each library item into the wrapper. However, Flex is NOT a part of our team’s workflow and I don’t want to introduce it to everyone building skins. Considering the platform, the surely is a way to solve the problem with just the Flash IDE, right?
That is simple yet great solution along with good description.
I wasted 4-5 hours to get help on Adobe’s site, blog, help files etc but failed to get any help.
Thank you so much for posting such a great solution.
Thanks for this great technique.
I’m able to load a symbol this way, and once it’s loaded I’m able to gotoAndStop on a certain label/frame within the attached symbol… but once the playhead get there there is another nested clip (named) and I’d like to use a goto within the nested clip… but this is not working. Tracing the nested clip yields undefined. Anybody have an idea how to access this nested clip?
Thanks,
JM
[...] Here is an article about it! [...]
this works perfect. I am able to access buttons and MovieClips from library, please tell me how I can get Graphic assets, as they can’t be export for actionscript. Do I need to make them as MovieClip only.
@JM do you get any solution for your problem ?
Cool……….
Than a Lot n Lot.
i am new bee to AS3. i am trying this over 3 days….
it showd undefied property even i am following the techniques which is showd in some website… At last you made it…
thanks
great tutorial, but how do I instantiate a bitmapData library object within the imported SWF ?
I tried simply replacing the object type(MovieClip) with BitmapData within this line: var mySquare:MovieClip = new Square() as MovieClip; but it did not work.
thanks for any help.
Just been trying this out independently and came across this post. It’s pretty neat but I kinda want to take it a bit further. Basically I have a load of SWFs with different assets in them. So I load them in and then I can access the asset classes. Magic! But… it gets really boring having to type out all of the gefDef stuff for each class. I could load an XML file containing those names, but it’d be neater to be able to quiz the applicationDomain or the SWF and find out all of the classes within. Anyone found a way to do this??
If these guys are listening…
@Michael Prescott - you’d need to define a new applicationDomain for each of your skins. That way the new definition will be kept separate from the old one. smthg like:
var appDomain1:ApplicationDomain = new ApplicationDomain();
loader.load(url, appDomain);
etc.
@phil - You can’t access the bitmapData directly, so what you need to do is wrap it in a MovieClip, then draw that to a bitmapdata object. This will work:
var Square:Class = appDomain.getDefinition(”Square”
as Class
var square:MovieClip = new Square() as MovieClip;
var squarebmd:BitmapData = new BitmapData(square.width, square.height);
var squareBM:Bitmap = new Bitmap(squarebmd);
squarebmd.draw(square);
addChild(squareBM);
that’s not supposed to be a smiley btw!