Introduction
I write this article because it seems to me that quite a few people still have no idea how to duplicate external assets (such as duplicating MovieClips, for example). Thus, I decided to take this opportunity despite there being quite a few articles that document this. If I haven’t bored your by now, read on!
First, Some History
Assets in Flash are normally quite coupled with the SWF. This is probably a result of the long years of timeline scripting that has been burnt into our memories. In Actionscript 2.0, however, the process of abstracting Assets from Actionscript was slightly alleviated through the copious use of duplicateMovieClip(). I too, used to be an avid user of duplicateMovieClip(), until I made the transition to Actionscript 3.0, But before then, duplicateMovieClip() was my quick and dirty solution to many situations I came across in Flash.
Abstracting Assets from Actionscript in Actionscipt 2.0 was relatively simple. One need only separate his/her assets into separate swf files, and simply loadClip() them into the SWF, and employ the use of duplicateMovieClip(). In Actionscript 3.0, however, this situation has changed as Adobe saw fit to remove (or shall we say, omit) the duplicateMovieClip() method.
Now, Adobe recommends the use of the “new” keyword to instantiate multiple instances of a Class. From the Actionscript 2.0 Migration reference:
In ActionScript 3.0, use the new operator to create a new instance.
This doesn’t really help us abstract our Assets from Actionscript. Most of us who have played around enough in Actionscript 3.0 would know that this requires that the Class be part of the SWF calling it, and in facts forces us to embed our assets within the SWF. Using the Loader class to import external SWFs does not expose any classes that we may use to invoke the “new” operation. At least, not obviously, as we shall see later.
Asset Libraries
Today we will go through a step-by-step process on how to create an Asset Library. Note, of course, that this isn’t the only way to handle External Assets, but this is the way I like to do it.
Creating an Asset
First things first, we create an Asset. This can be anything from a Shape to a Sprite.
At this point you need to note the “Class” field. This field is important as it is how you will be referencing this class externally.
Next, click OK, and ignore the prompt that pops up when you do. Now, you may export your SWF (Lets call it “circle.swf”). Once that is done, start another SWF, which is where we will try to load our external Asset.
Loading an Asset
Now we get to the nitty gritty of it all. First we need to create some code that will load our Circle, so that we can duplicate it. Let us create an event handler too, to handle Event.COMPLETE.
Above you see some code for loading external SWFs using the Loader class. So now what we are going to do is use what is called the ApplicationDomain to get the class definitions from within Circle.swf!
Lets explain what we did there. We accessed the ApplicationDomain of the SWF we loaded, and then we got the definition of the Class we wanted, in this case the Circle class we did in the first SWF. Note that the name of the definition has to be exactly the same.
Now let us compile, and see what we get!
Closing
Using this technique, it is simple to create a library of assets, that you can load into your main SWF and duplicate them! While this isn’t the only technique, nor the only method of utilising the ApplicationDomain, this is (in my humble opinion) the best way to go about Abstracting Assets from Actionscript.
Links
Here are some links if you want more information, as well as a zip of source code so that you may look at the code directly.
http://blogs.adobe.com/rgonzalez/2006/06/applicationdomain.html
http://livedocs.adobe.com/flex/2/langref/flash/system/ApplicationDomain.html
Asset Libraries Tutorial Files











Hi AngelStreet,
have you actually writtened/assigned/linked classes to the moviesclips? This has to be done as there is no way to put actionscript within movieclips anymore.
The main problem here, I think, is that the loader’s content is still a movieclip class no matter what. So even if you grab the constructor of the loader’s content, you’ll just be getting the MovieClip constructor. Either do it my way, or one of the comments above may also help you.
Otherwise, you should try to limit the number of actionscript that goes into your loaded content.
You’re right I have not touched ActionScript in quite awhile, but I hope this message helps.
Cheers
Daryl
Thanks very much for the info. After googling this topic for the whole morning, it’s the only simple and easy approach that I could find.
there is no duplicating here. it is creating.
hello to every one – hope yous had a good xmas – pity we didnt get snow was all prepared wi sledges kids loving it any ways , all the best for the comming year – michael