Library & LibraryManager

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.

36 Responses to “Library & LibraryManager”

  1. [...] 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 [...]

  2. chao says:

    hey Tink….nice library, may I ask where is the repository? doesn’t seems on the google code

  3. Tink says:

    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

  4. Justin says:

    Hi

    Library is seriously very handy, great work, thanks for releasing.

  5. Chris says:

    no trunk? This is a great tool, I just don’t understand why I’m getting a LibraryEvent Error

  6. Tink says:

    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).

  7. Dongland says:

    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?

  8. Tink says:

    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.

  9. Dongland says:

    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

  10. Dongland says:

    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

  11. Robert says:

    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.

  12. Tink says:

    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 );

  13. But your Google code repository doesn’t offer any downloads

    http://code.google.com/p/tink/downloads/list

  14. arash says:

    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.

  15. Seb says:

    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

  16. Tink says:

    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.

  17. arash says:

    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 ?

  18. Tink says:

    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.

  19. jo says:

    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 //

  20. Raff says:

    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

  21. Tink says:

    Hey Ronan, try

    library.loadSWFS.apply( this, yourArray );

  22. [...] Library & LibraryManager – For using external assets in your flash projects.http://www.tink.ws/blog/library-librarymanager/ [...]

  23. [...] was trying to make it so that I can use a library manager to load the files dynamically. Question by This question originated from [...]

  24. [...] Library & LibraryManager – For using external assets in your flash projects. http://www.tink.ws/blog/library-librarymanager/ [...]

  25. [...] Library & LibraryManager – For using external assets in your flash projects. http://www.tink.ws/blog/library-librarymanager/ [...]

  26. Scott says:

    Hi,

    This looks very good, thanks for sharing!

    The problem I am having is related to application domains. I am wannting to load a theme.swf, which contains all the graphics for an app, into a seperate app domain so I can easily garbage collect it by removing the .swf and setting the domain it is loaded it into to to null. This also means I can load and unload themes easily….however I am having security issues when debugging locally. See my post here: (http://forums.greensock.com/viewtopic.php?f=6&t=4509&sid=4cd5423e4b406d142e5caf6a58f5bfd1)

    How would this work with your LibraryManager, if I load a theme.swf and then load a theme2.swf (will all same names) would I simply tell my application to look at the classes in the new library instead? Is it easy to remove a library?

    Sorry if i’m way off the mark with this approach.

  27. Tink says:

    Well this is going to solve any of your ApplicationDomain issues, but it is pretty easy to remove a Library

    LibraryManager.libraryManager.removeLibrary( name:String, destroy:Boolean = false ):void

    The Library will clear up after itself, but instance created will not be garbage collected if other items have references to them.

  28. Scott says:

    Thanks for the reply. Is the doumentation or examples for the library manager still available, I can’t find them on your repo?

  29. [...] Library & LibraryManager – For using external assets in your flash projects. http://www.tink.ws/blog/library-librarymanager/ [...]

  30. [...] Library & LibraryManager – для использования внешних файлов в Flash проектах. [...]

  31. [...] Library & LibraryManager – For using external assets in your flash projects. http://www.tink.ws/blog/library-librarymanager/ [...]

  32. [...] отслеживания события зелчка правой кнопкой мышки в AS3. Library & LibraryManager – для использования внешних файлов в Flash проектах. [...]

Leave a Reply