Let’s say you build a simple animation in CS5.5 – like a loading indicator – how do you embed and use it in your Flex or Flash application?

First, let’s take a look at the animation. Assuming you don’t need to load it at runtime (and it’s a reasonable size), you can easily embed a SWF animation in a custom class:

[Embed(source="/loading.swf", mimeType="application/octet-stream")]
public var LoadingAnimation : Class;


Specifying a mimeType of “application/octet-stream” is important here, because it allows any file type to be used and gives you a ByteArray for the embedded object.

Next, you need to create a LoaderContext in order to use the embedded SWF as an animation and to execute code (if needed):

// allows us to import SWFs to use as animations
var context : LoaderContext = new LoaderContext (false,
    ApplicationDomain.currentDomain);
context.allowCodeImport = true;

var loader : Loader = new Loader ();
loader.loadBytes (new LoadingAnimation (), context);


Once you’ve loaded the embedded animation, simply add the Loader object to the stage and you’re done!