Embedded asset classes

ActionScript 3.0 uses special classes, called embedded asset classes, to represent embedded assets. An embedded asset is an asset, such as a sound, image, or font, that is included in a SWF file at compile time. Embedding an asset instead of loading it dynamically ensures that it will be available at run time, but at the cost of increased SWF file size.

Using embedded asset classes in Flex

To embed an asset in ActionScript code, use the [Embed] metadata tag. Place the asset in the main source folder or another folder that is in your project's build path. When the Flex compiler encounters an Embed metadata tag, it creates the embedded asset class for you. You can access the class through a variable of data type Class that you declare immediately following the [Embed] metadata tag. For example, the following code embeds a sound named sound1.mp3 and uses a variable named soundCls to store a reference to the embedded asset class associated with that sound. The example then creates an instance of the embedded asset class and calls the play() method on that instance:

<<below code sample was double conditioned to FlexOnly+No Trans. I had to emove FlexOnly for Flex 2.0.1 loc prodeuction. Since this book is in Flex 2.0.1 location and should not be used for Flash-MN>>

package
{
    import flash.display.Sprite;
    import flash.media.SoundChannel;
    import mx.core.SoundAsset;

    public class SoundAssetExample extends Sprite
    {
        [Embed(source="sound1.mp3")]
        public var soundCls:Class;
        
        public function SoundAssetExample()
        {
            var mySound:SoundAsset = new soundCls() as SoundAsset;
            var sndChannel:SoundChannel = mySound.play();
        }
    }

}

TIP

 

To use the [Embed] metadata tag in an Adobe Flex Builder 2 ActionScript project, you must import any necessary classes from the Flex framework. For example, to embed sounds, you must import the mx.core.SoundAsset class. To use the Flex framework, include the file framework.swc in your ActionScript build path. This will increase the size of your SWF file.

Alternatively, you can embed an asset with the @Embed() directive in an MXML tag definition. For more information, see About embedding assets in the Flex 2 Developer's Guide.


Flex 2.01

Take a survey


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flex/201/html/04_OO_Programming_161_08.html