View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-13 #28 Old  
025 - Shared Library, go away! (AS3)
 
Shared Libraries can be summed up in four words: pain in the ass. They can still be used with AS3 but why bother with them when there is an easier way to pull library assets from SWF files!

Let's say that you have a SWF and in that SWF file's library is a sound file linked to the first frame of the movie with the class name "MySound". The following code demonstrates how to load that SWF file into another SWF file, grab the sound from the library, and then play the sound. Woohoo!

ActionScript Code:
  1. import flash.events.Event;
  2. import flash.net.URLRequest;
  3. import flash.display.Loader;
  4. import flash.media.Sound;
  5.  
  6. var req:URLRequest = new URLRequest("library.swf");
  7. var loader:Loader = new Loader();
  8. loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLibraryLoaded);
  9. loader.load(req);
  10.  
  11. var mySound:Sound;
  12.  
  13. function onLibraryLoaded(e:Event):void
  14. {
  15.     var cls:Class = Class(loader.contentLoaderInfo.applicationDomain.getDefinition("MySound"));
  16.     mySound = new cls();
  17.     mySound.play();
  18. }
This method can probably be used for most of the library assets you can store in a SWF file. I think you might even be able to use this with fonts but I haven't tried that yet.

Have fun!