AS3 Metadata Tags
In AS3, metadata tags play a much more important role than they did in AS2. Here are some of the biggies along with samples of their usage.
SWF
-
[SWF(backgroundColor="#FFFFFF", frameRate="31", width="550", height="400")]
You can use the SWF tag after your imports, but before your class definition. These settings will override any MXML compiler options, however if you compile from the Flash IDE, these settings will be overriden by the settings in the properties panel. The advantage of using this tag is that your settings stay independent of your build files.
Embed
Here's an example of embedding an image into the SWF file.
-
[Embed(source="assets/images/background.jpg")]
-
protected var Background:Class;
Now, to use that image:
-
var objBackground:Bitmap = new Background();
What's even cooler is that you can create SWF files that are used strictly for their library much like you would with runtime sharing setups.
-
[Embed(source="icon_library.swf", symbol="play_icon")]
-
private var PlayIcon:Class;
-
-
[Embed(source="icon_library.swf", symbol="pause_icon")]
-
private var PauseIcon:Class;
Really useful stuff.
ArrayElementType
You can use this tag to have the MXML compiler perform type checking on the elements in the array.
-
[ArrayElementType("String")]
-
protected var m_aBookTitles:Array;
Frame
Keith Peters posted a really detailed write-up of how to use this tag for preloading a few months ago. Basically, the deal is that you can use this tag to force the compiler to create a two-frame SWF file.
-
[Frame(factoryClass="com.boostworthy.apps.AppFactory")]
The reason you would want to do this is so that you have minimal logic (ideally just the preloader code) on the first frame and everything else on the second. So you place the frame tag above your class definition in your 'Main' class, then all the preloader logic in the factory class.
Event
The last tag I want to mention is the event tag. This is more of a Flex-compatibility tag. If you try and use an event name in MXML without declaring the event with an event tag, the MXML compiler will throw an error. So here's how to avoid that:
-
// In this sample, the 'AnimationEvent.START' event is being declared. The 'START' constant's value is 'animation start'.
-
[Event(name="animation start", type="com.boostworthy.animation.events.AnimationEvent")]
As with the frame and SWF tags, event tags should be placed after imports, but before the class definition.
5 Comments so far
Leave a reply

Hi Ryan!
Is this possible to make Flash IDE compiler to interpret Metadata tags?
I was trying to make SWF self-preloadable (as Keith's example) but with no success. It looks like tags are being omitted. Using [Frame(factoryClass="com.boostworthy.apps.AppFactory")] won't get any compiler error, when it should - I don't have that class in the classpath.
Even when the factoryClass is correct it also seems to be ignored by skipping the constructor execution.
What's wrong? Thanks!
@Og2t - A good chunk of the AS3 metadata tags are intended to be used with the tools included in the Flex SDK and are ignored by the the Flash IDE compiler. The 'Frame' tag is one of them, however you don't really need it if you are using the Flash IDE. The 'Frame' tag basically just places all non-preloader code and assets onto a second frame for you so that your preloader will load right away and behave correctly. Since the Flash IDE has a physical timeline, you can do this yourself as you see fit. Hope that helps!
Do you know which metadata tag are NOT ignored by the Flash IDE compiler? That bit of info would be very useful to know. Thanks!
@Og2t - Flash CS4 can use the Flex SDK to build swf files, thus allowing to use the 'meta' embeds from within the Flash IDE too. More here.
Hi Ryan,
I wonder what you mean by using the timeline in Flash IDE? How can I assign the 2 classes to 2 separate frames?