This originally appeared on my blog in September 2007.
I've been fighting this a bit today, and finally got something working, so I thought I'd set it down.
I needed to package a bunch of external MovieClips, sounds etc. into an external library .swf for loading at runtime; and then to be able to extract them by some sort of ID (akin to the old symbol name in AS2).
This all needs to be done for the Flex SDK.
It goes something like this:
Create a Resources.as file:
package { import flash.display.Sprite; // This _must_ inherit from Sprite, or the resources can't be attached to // the main movie. public class Resources extends Sprite { // List your resources here, as public variables [Embed(source="mysource.swf",symbol="AnySymbol")] public var SomeSymbol1:Class; [Embed(source="myOtherSource.swf",symbol="AnotherSymbol")] public var SomeSymbol2:Class; } }
mxmlc Resources.as -output resources.swf -sp . -use-network=false
-use-network - you might not, depending on your settings for the host app.)mxmlc to compile root classes derived from mx.modules.Module or mx.core.Application. Obviously not!)
Then load it into your app using a standard flash.display.Loader, without specifying an applicationDomain. Once loaded, get at the assets like this:
var resourceClass:Class = loader.contentLoaderInfo.applicationDomain.getDefinition("Resources") as Class; var resources:Object=new resourceClass(); var symClass1:Class=resources['SomeSymbol1'] as Class; var symbol1:MovieClip=new symClass1() as MovieClip; var symClass2:Class=resources['SomeSymbol2'] as Class; var symbol2:MovieClip=new symClass2() as MovieClip;
Yes, it does seem simple. But it's been quite painful, this morning - the documentation is quite misleading.