Here’s and example of our Library & LibraryManager classes that we use in some of our Flex and AS 3.0 projects to manage our external assets stored in SWF’s.
The classes enabled you to create multiple libraries of embedded (retaining and giving access to code) or loaded SWF’s.
You can create instance of Library wherever you want, but you can also create them through the LibraryManager giving you a single class to gain access to all your Library instances.
Here’s a quick example creating 2 Library instances using the LibraryManager. Each Library contains 2 SWF’s, each containing a BitmapData and MovieClip which are created using the Library. One Library instance embeds the SWF’s the other loads them. Not shown in the example, but you can also have a mix of loaded and embedded SWF’s in the same Library, and they contain fonts as well as custom classes.
LibraryExample
LibraryExample source (NOTE: The source has already been edited but not updated here. I suggest you pull down the latest version from our google code repository)
You can obviously get it one our Google Code repository.

[...] y and LibraryManager for Assets and Other AS3 Utilities May 26, 2008 — drawk Tink posted a great library manager for using external assets in your flash proje [...]
hey Tink….nice library, may I ask where is the repository? doesn’t seems on the google code
you can check it out here
# Non-members may check out a read-only working copy anonymously over HTTP.
svn checkout http://tink.googlecode.com/svn/trunk/ tink-read-only
and browse here http://code.google.com/p/tink/source/browse
Hi
Library is seriously very handy, great work, thanks for releasing.
no trunk? This is a great tool, I just don’t understand why I’m getting a LibraryEvent Error
There’s a few reasons why you would get an error.
1. You try to create an instance of LibraryManager instead of using LibraryManager.libraryManager. (LibraryManagerError – ‘LibraryManager is to be used as a Singleton and should only be accessed through LibraryManager.libraryManager.’)
2. You try to add a new Library with the same name of a Library currently stored in the LibraryManager. (LibraryManagerError – ‘There is already a library with this name. Libraries stored in the LibraryManager must be unique names.’)
3. You try and remove a Library, that the LibraryManager doesn’t contain. (LibraryManagerError – ‘The supplied library name could not be found in the LibraryManager.’)
4. You try to get the definition from a Library, which doesn’t contain the definition (ReferenceError).
It’s great job. think for posting.
I have a question. If I imported three kind of SWF files at the LibraryManager then all files have same name of Linkage.
Like this~!
a.swf -> linkageMovieClip(name:test)
b.swf -> linkageMovieClip(name:test)
c.swf -> linkageMovieClip(name:test)
Same linkage name
How can I use to select a Linkaged MovieClip at this situation?
Think of a Library like a library in Flash, i.e. you can’t have items names the same.
What you can do with LibraryManager is create as many libraries as you wanted so that each can have items that are name the same in them.
For instance for a game you might create a ‘Level1′ and ‘Level2′ etc library. Each or these could have an assets named ‘Flower’, but each actually refer to a different asset.
I had a lot of time think about those issues.
I realize what you say~
Last time I thought. LibraryManager might be differently managing
with flash Library system. so I could’t understand. Why it is?
But everything is clear Now.
It’s very helpful Class.
Thank you
I realize what you say~
Last time I thought. LibraryManager might be differently managing
with flash Library system. so I could’t understand. Why it is?
But everything is clear Now.
It’s very helpful Class.
Thank you
I’m loving this class, will be handy in my upcoming projects.
One thing though, could you consider adding an addLoader(loader:Loader) method to the Library class for SWFs that have already been loaded (ie. through BulkLoader and other APIs)?
Cheers.
Hey Robert
“One thing though, could you consider adding an addLoader(loader:Loader) method to the Library class for SWFs that have already been loaded (ie. through BulkLoader and other APIs)?”
Can you give me a quick user-case? Wouldn’t u just load the SWF’s through the Library anyway?
Library.loadSWFS( url0, url1, url2, url3, url4, urletc );
But your Google code repository doesn’t offer any downloads
http://code.google.com/p/tink/downloads/list
You need to download via SVN
http://code.google.com/p/tink/source/checkout
hi
i want to know if it’s possible that we put .swf file in a sqlite db and
load them as binary data and use their library?
thx.
Hi,
I get the following error from the cast to ByteArray in Library’s embedSWFS():
Error: Error #2136: The SWF file http://localhost:8888/myProject/mySWF.swf contains invalid data.
I am trying to embed a Flash generated SWF containing 5 icons.
Any ideas?
Thanks
Hey Seb
Sometimes the content of the SWF get corrupted. I’m not actually sure how you can tell when an SWF is corrupt for this type of load process, and the SWF would play fine if loaded normally.
I have got round this in the past by re-generating the SWF. Create a new FLA, and copy over as much as the raw assets as possible to avoid copying the corrupt part over (i.e. don’t just copy the clips over, instead create new clips and paste in their content.
Thanks for reminding me about this, i’ll see if I can get some feedback from Adobe on it at FOTB.
hi tink
I test the cast to a class when the byteArray load complete .but the as engine
set an Id for a class that uses in a swf and that class in the system that load
so it couldn’t cast the data from the files loaded to a class that exist in the system
this problem does n’t exist in another language like java.
i don’t know if this problem maybe solved in cs4 ?
Hey Arash
You cannot cast anything to a class unless it is, or extends that class.
If you define that class, as the base class for the asset you are loading in the Flash IDE, it should cast fine, once in the Library.
That said a better approach is to write classes that require graphic assets, the use the IDE to create these assets, load them with the library and cast them as MovieClips, then pass them through to you class as the graphic asset.
hello tink,
thanks for sharing your source, it’s great.
but it looks like you’ve missed something.
ws.tink.managers.removeLibrary
it throws an error if i try to remove a library. maybe you have to change it this way.
chears
var index:int = getLibraryIndex( name );
if( index != -1 )
{
var library:Library = Library( _libraries.splice( index, 1 )[ 0 ] );
if( destroy ) library.destroy();
}
else //
Hi Tink,
Just wanting to use the manger to dynamicly load in fonts that could be set via a flash var, so that I can seperate out the display from the logic. I was going to pass in a list of fonts used (to be loaded by the manager) and then use a style sheet to set the font.
The problem I have is the use of ..args on the loadSWFS() function I’d like to be able to pass an array of URLS to this. I have modified the function to test if an argument is an array and then I load that but I just thought I’d suggest it as I’d like to stay up to date with SVN.
Thanks
Ronan
Hey Ronan, try
library.loadSWFS.apply( this, yourArray );
nice one