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(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.
Here’s an example of embedding an image into the SWF file.
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.
You can use this tag to have the MXML compiler perform type checking on the elements in the array.
- protected var m_aBookTitles:Array;
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.
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.
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.7 comments