16 February 2009
Prior experience writing ActionScript 3.0 is recommended, basic familiarity with the Flash authoring environment, and working with the Flex 3 SDK is also helpful.
Intermediate
This section describes how to use the [Embed] metadata tag in Flash CS4 Professional to embed GIF, PNG, JPEG, or MP3 files. The first example takes a look at the process for adding an embedded GIF file.
If you haven't already, be sure to download the sample files available on the first page of this article. To follow along with the provided examples, uncompress the ZIP file and open the folder named \gif to examine the sample files:
package {
import flash.display.*;
public class GIFEmbed extends Sprite {
[Embed(source = "../assets/talapetra.gif")]
private var theClass:Class;
public function GIFEmbed () {
var displayObj:DisplayObject = new theClass();
addChild (displayObj);
}
}
}
The highlighted line of code includes a source attribute that specifies the name and path of the asset to embed. You can use an absolute path or a document-relative path to the file containing the embed statement. In this example, the ActionScript class named GIFEmbed.as is navigating up one directory to locate the folder named \assets that contains the talapetra.gif image file.
Note: The [Embed] tag for metadata can also take another optional attribute, mimeType, that allows you to specify the MIME type of the linked asset. I'll discuss this attribute in more detail later.
The order of the ActionScript code is important. You must add the [Embed] metadata tag before declaring the variable, and that variable has to be of the type Class. In the code snippet below, the [Embed] metadata tag is used before a private variable called theClass of type Class is declared:
[Embed(source = "../assets/talapetra.gif")]
private var theClass:Class;
Inside the function named GIFEmbed, a new variable named displayObj of the type DisplayObject is used to instantiate a new DisplayObject from theClass. The next line uses the addChild method to add the displayObj to the display list and render it on the Stage:
var displayObj:DisplayObject = new theClass();
addChild (displayObj);
Refer to the sample files to see the folder structure and identify the path between the GIFEmbed.as class file and the GIF image file named talapetra.gif (located in the \assets folder).
An error message appears because the project is attempting to use features in Flash CS4 that require some additional Flex classes. Flash detects the missing Flex classes and displays the message in Figure 2.
In the dialog box, you have the option to set the path to the Flex SDK, but it is not necessary to browse and locate it because the path is already set. All you need to do is click the Update Library Path button to ensure that the correct path for the flex.swc ($(FlexSDK)/frameworks/libs/flex.swc) is automatically added to the Library path of the current FLA file.
Note: After running the SWF for the first time, the Flex SDK Required window no longer appears. Instead, you'll see compiler errors regarding the missing classes (see Figure 3).
As I mentioned previously, the [Embed] metadata tag can take two attributes:
symbol keyword to specify the name of the symbol in a SWF to embed.Flash CS4 Professional supports the same set of MIME types that Flex supports:
Similar to the process previously listed, metadata can also be applied to a frame script. In this section, I describe how to use the [Embed] tag to embed metadata in a frame script. If you are following along with the sample files, refer to the files in the \Embed on Frame Script folder:
[Embed(source="../assets/talapetra.gif")]
var theClass:Class;
var displayObject:DisplayObject = new theClass();
addChild(displayObject);
This section describes how to use the [Embed] metadata tag in Flash CS4 Professional to embed GIF, PNG, JPEG, or MP3 files. The first example takes a look at the process for adding an embedded GIF file.
If you haven't already, be sure to download the sample files available on the first page of this article. To follow along with the provided examples, uncompress the ZIP file and open the folder named \gif to examine the sample files:
package {
import flash.display.*;
public class GIFEmbed extends Sprite {
[Embed(source = "../assets/talapetra.gif")]
private var theClass:Class;
public function GIFEmbed () {
var displayObj:DisplayObject = new theClass();
addChild (displayObj);
}
}
}
The highlighted line of code includes a source attribute that specifies the name and path of the asset to embed. You can use an absolute path or a document-relative path to the file containing the embed statement. In this example, the ActionScript class named GIFEmbed.as is navigating up one directory to locate the folder named \assets that contains the talapetra.gif image file.
Note: The [Embed] tag for metadata can also take another optional attribute, mimeType, that allows you to specify the MIME type of the linked asset. I'll discuss this attribute in more detail later.
The order of the ActionScript code is important. You must add the [Embed] metadata tag before declaring the variable, and that variable has to be of the type Class. In the code snippet below, the [Embed] metadata tag is used before a private variable called theClass of type Class is declared:
[Embed(source = "../assets/talapetra.gif")]
private var theClass:Class;
Inside the function named GIFEmbed, a new variable named displayObj of the type DisplayObject is used to instantiate a new DisplayObject from theClass. The next line uses the addChild method to add the displayObj to the display list and render it on the Stage:
var displayObj:DisplayObject = new theClass();
addChild (displayObj);
Refer to the sample files to see the folder structure and identify the path between the GIFEmbed.as class file and the GIF image file named talapetra.gif (located in the \assets folder).
An error message appears because the project is attempting to use features in Flash CS4 that require some additional Flex classes. Flash detects the missing Flex classes and displays the message in Figure 2.
In the dialog box, you have the option to set the path to the Flex SDK, but it is not necessary to browse and locate it because the path is already set. All you need to do is click the Update Library Path button to ensure that the correct path for the flex.swc ($(FlexSDK)/frameworks/libs/flex.swc) is automatically added to the Library path of the current FLA file.
Note: After running the SWF for the first time, the Flex SDK Required window no longer appears. Instead, you'll see compiler errors regarding the missing classes (see Figure 3).
As I mentioned previously, the [Embed] metadata tag can take two attributes:
symbol keyword to specify the name of the symbol in a SWF to embed.Flash CS4 Professional supports the same set of MIME types that Flex supports:
Similar to the process previously listed, metadata can also be applied to a frame script. In this section, I describe how to use the [Embed] tag to embed metadata in a frame script. If you are following along with the sample files, refer to the files in the \Embed on Frame Script folder:
[Embed(source="../assets/talapetra.gif")]
var theClass:Class;
var displayObject:DisplayObject = new theClass();
addChild(displayObject);
In the previous section, I reviewed the process for using the [Embed] tag to embed the metadata of an image using a GIF file. Displaying external data is very useful, but there are many other possibilities available when using the [Embed] metadata tag. This section describes the steps to embed a whole SWF file or embed a specific symbol from a SWF file.
If you are following along with the downloadable sample files, refer to the files in the folder named \SWF with slice 9. Follow these steps:
Note: For this exercise, also ensure that the Enable Guides for 9-Slice Scaling option is checked.
package {
import flash.display.*;
import flash.events.MouseEvent;
import flash.geom.Rectangle;
public class SWFEmbed extends MovieClip {
public var displayObj:DisplayObject;
[Embed(source="Movie.swf", symbol="Slice9")]
var theClass:Class;
public function SWFEmbed() {
displayObj = new theClass();
addChild(displayObj);
displayObj.x = 200;
displayObj.y = 220;
}
}
}
In this code, the [Embed] metadata tag uses the source parameter to specify the name and path to the SWF file that will be embedded. The symbol parameter specifies the name of the specific symbol to be embedded from the SWF that is referenced in the source parameter.
In addition to embedding image files, SWFs, and symbols from SWFs, the [Embed] metadata tag has the ability to embed OpenType and TrueType fonts into a Flash file. In this section, I illustrate how to use the [Embed] metadata tag to embed fonts in Flash CS4. This article assumes that you have Arial Bold already installed on your system. If you don't have it, you can buy it from SearchFreeFonts.com or similar sites.
Note: The [Embed] metadata tag supports only classes and member variables, so if you attempt to use the [Embed] tag on a function, the following compiler error will occur:
"Embed is only supported on classes and member variables."
If you are following along, refer to the folder named \TrueType in the sample files:
package {
import flash.text.*;
import flash.display.MovieClip;
public class FontClass extends MovieClip {
[Embed(source="Arial Bold.ttf",
fontName="myFont", fontWeight="bold", advancedAntiAliasing="true",
mimeType="application/x-font")]
private var theClass:Class;
public function FontClass () {
var t:TextField=new TextField();
t.embedFonts = true;
var textFormat:TextFormat=new
TextFormat();
textFormat.size = "30";
textFormat.font = "myFont";
t.text = "[Embed] metadata rocks!!!";
t.width = 500;
t.setTextFormat (textFormat);
addChild (t);
}
}
}
In the highlighted code above, the following parameters inside the [Embed] metadata tag are used to embed an Arial bold font:
systemFont parameter instead of source.Note: If the font has a weight and you do not include the fontWeight parameter in the [Embed] metadata tag, you'll receive the following compiler error when the SWF is tested:
Exception during transcoding: Font for alias 'myFont' with plain weight and style was not found at...
along with the path to the embedded font (see Figure 11).
The same type of error results if the embedded font has a specific style (such as italic) that is not indicated by using the fontStyle parameter in the [Embed] metadata tag. Be sure to specify all of the necessary parameters when embedding fonts with metadata.
In the earlier sections of this article, I reviewed the process for using the [Embed] metadata tag to embed graphic files, SWF files, symbols from SWF files, and fonts into a SWF. All of those possibilities are very useful, but there is another type of file that can also be embedded in a SWF using the [Embed] metadata tag: XML files.
I saved the best for last because this functionality is extremely helpful for bringing in external data to SWFs—and is straightforward to accomplish. You can do some very interesting things with embedded XML data, so this is a great tip to keep in mind for future projects.
The following example provides instructions for embedding an XML file using the [Embed] metadata tag. If you are following along, refer to the folder named \XML in the sample files:
package {
import flash.display.*;
import flash.utils.ByteArray;
public class XMLLoader extends Sprite {
[Embed(source = "training.xml",mimeType = "application/octet-stream")]
private var theClass:Class;
public function XMLLoader (){
var xmlObj:Object = new theClass();
trace(xmlObj);
}
}
}
Note: When embedding XML data, you must set the mimeType parameter to "application/octet-stream". Flash is not able to detect the correct MIME type by the XML file extension, so make sure to set the mimeType parameter whenever you embed XML data.
To see other examples of working with the [Embed] metadata tag, see Loading or embedding a shader in the "Working with Pixel Bender shaders" section of the Programming ActionScript 3.0 for Flash documentation. Also check out Using Flex metadata in the "ActionScript" section of the Using Flash CS4 Professional documentation.
See also the Flash Authoring blog for more details, as well as Peter deHaan's blog entry: Using Flex classes in Flash CS4. Be sure to visit the Flash Developer Center and Flex Developer Center to find more articles, tutorials, and sample projects.

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License
| 04/23/2012 | Auto-Save and Auto-Recovery |
|---|---|
| 04/23/2012 | Open hyperlinks in new window/tab/pop-up ? |
| 04/21/2012 | PNG transparencies glitched |
| 04/01/2010 | Workaround for JSFL shape selection bug? |
| 02/13/2012 | Randomize an array |
|---|---|
| 02/11/2012 | How to create a Facebook fan page with Flash |
| 02/08/2012 | Digital Clock |
| 01/18/2012 | Recording webcam video & audio in a flv file on local drive |