boostworthyisryantaylor

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

[AS]
[SWF(backgroundColor="#FFFFFF", frameRate="31", width="550", height="400")]
[/AS]

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.

[AS]
[Embed(source="assets/images/background.jpg")]
protected var Background:Class;
[/AS]

Now, to use that image:

ActionScript
< view plain text >
  1. 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.

ActionScript
< view plain text >
  1. [Embed(source="icon_library.swf", symbol="play_icon")]
  2. private var PlayIcon:Class;
  3.  
  4. [Embed(source="icon_library.swf", symbol="pause_icon")]
  5. 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.

ActionScript
< view plain text >
  1. [ArrayElementType("String")]
  2. 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.

ActionScript
< view plain text >
  1. [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:

ActionScript
< view plain text >
  1. // In this sample, the 'AnimationEvent.START' event is being declared. The 'START' constant's value is 'animation start'.
  2. [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.

7 comments

7 Comments so far

  1. Og2t February 6th, 2008 1:22 pm

    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!

  2. Ryan Taylor February 7th, 2008 3:02 am

    @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!

  3. as3fan August 6th, 2008 1:30 pm

    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!

  4. jayesh August 18th, 2009 6:47 am

    @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.

  5. Peter Schum February 25th, 2010 11:53 pm

    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?

  6. [...] AS3 Metadata Tags [...]

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Anti-spam image