Friday, January 12th, 2007

Category: FlashCategory: Flex

AS3 Projects and the SWF Metadata tag

While most people are probably building Flex projects, there are some of us who build AS-only projects as well. While looking for resources on AS3 I came across Joey Lott’s ActionScript 3 Cookbook. Near the beginning of the book there is a section labelled “Customizing the Properties of an Application”. That caught my attention and I came across the SWF metadata tag.

The Flex docs don’t mention the SWF tag, but it does work.

The docs only mention the compiler arguments that you can use to adjust the stage properties, which is fine, but having control in the AS source is MUCH better. It makes your project easier to port to another system.

When I found this tidbit in the AS3 Cookbook and tried it I was dismayed – it didn’t work! In the book it mentions that the above code is placed in the main package, outside of your classes, before or after any imports.

However, from my tests, the SWF tag must be placed AFTER the imports.

For example:

package
{
	import flash.display.Sprite;

	[SWF(width='400', height='300', backgroundColor='#ffffff', frameRate='30')]

	public class test_ASonly extends Sprite
	{
		public function test_ASonly()
		{
			trace(stage.frameRate); // 30
		}
	}
}

10 Responses to 'AS3 Projects and the SWF Metadata tag'

Subscribe to comments with RSS or TrackBack to 'AS3 Projects and the SWF Metadata tag'.

  1. Robert | Friday, March 2nd, 2007 | 6:46 PM

    Just ran across the same problem. Cheers for that.

  2. fleecie | Friday, June 8th, 2007 | 1:57 PM

    I knew about the SWF metadata tag from some where (but couldnt remember where) and I spend about three hours last night searching the docs and adobe support as I needed details of implimentation and legal arguments…

    finally I searched wider and found this… thanks you’ve pointed me back to the as3 cookbook where i must have seen it first.

    why not document this what seems very basic and useful feature
    grrrr – rant over and thank again

  3. Greg | Monday, November 5th, 2007 | 11:29 PM

    So, in Flash CS3 term, does the “main package” equals to the main document AS3 class script?

  4. Derek Vadneau | Monday, November 5th, 2007 | 11:53 PM

    No. The “main” or “default” package can contain any number of classes. The main document class is not a package.

    You can kind of think of the default package as being similar to _global from AS1/AS2. When you define a class on the default package it is accessible from any other class in any other package, without requiring an import.

    The default package is a double-edged sword. It is not recommended to use the default package when defining classes. Although, the main document class is a reasonable exception, imho.

  5. Greg | Tuesday, November 6th, 2007 | 1:17 AM

    So to get things straight, because Adobe did a shoddy explanation of what a package is… the main package is the one package sitting in the main project directory (ie. just package, not package com, not package.com.custom, etc), and there can only be one class file in that directory?
    Sorry, I’m pretty confused about this whole package she-bang… since, I think, in Flash CS3 I can only declare one class per file that is accessible by flash (which, has to have the same name as the filename itself), although the class can be “wrapped” inside a package block that can contain other classes that can be used by and only by the “main” class…. like I said, I’m thoroughly confused :P
    PS: the comment posting security code us giving me a lot of trouble, I have to post several times until I actually get it right (or it thinks that I got it right) :P

  6. Derek Vadneau | Tuesday, November 6th, 2007 | 10:22 AM

    I think most of those points are accurate, so you don’t sound too confused.

    If you want perhaps a better explanation, you should take a look at some of the AS3 books out there. Although I haven’t read it yet, Essential ActionScript 3 (EAS3) by Collin Moock would probably shed a lot of light on this.

    The default package is where you define the classpath(s). In Flash CS3 one of the default classpaths is the same directory as the FLA, so placing a class file in that directory defines the class as being in the default package. You could, however, change this so that the classpath for the FLA is some other directory. In that case the default package would be the directory you’ve chosen. You can also define your main document class in a different package.

    For example, let’s say we redefine the classpaths for our FLA so that the only location is a folder called “classes” that resides next to our FLA. In the classes folder we place a class file called Central.as. In Central.as we define the package as having a blank name, which is the default package. The name of the class is Central. We’ll create a directory called com\tracethis. In the tracethis folder we’ll create a file called Main.as. In Main.as we name the package as “com.tracethis” and name the class as Main. In Flash CS3 we define the Document Class as com.tracethis.Main. Inside of the Main class we can then target static methods within the Central class or instantiate Central and use any public members, all without having to specify an import for Central in Main.as.

  7. MyHieu | Wednesday, February 6th, 2008 | 1:27 PM

    thx trace(this).com, your experience is very helpful to me ^..^

  8. okz | Thursday, February 18th, 2010 | 4:25 AM

    Wow, respect!

  9. Arnold | Thursday, May 20th, 2010 | 4:23 AM


    I think most of those points are accurate, so you don’t sound too confused.

    If you want perhaps a better explanation, you should take a look at some of the AS3 books out there. Although I haven’t read it yet, Essential ActionScript 3 (EAS3) by Collin Moock would probably shed a lot of light on this.

    The default package is where you define the classpath(s). In Flash CS3 one of the default classpaths is the same directory as the FLA, so placing a class file in that directory defines the class as being in the default package. You could, however, change this so that the classpath for the FLA is some other directory. In that case the default package would be the directory you’ve chosen. You can also define your main document class in a different package.

    For example, let’s say we redefine the classpaths for our FLA so that the only location is a folder called “classes” that resides next to our FLA. In the classes folder we place a class file called Central.as. In Central.as we define the package as having a blank name, which is the default package. The name of the class is Central. We’ll create a directory called com\tracethis. In the tracethis folder we’ll create a file called Main.as. In Main.as we name the package as “com.tracethis” and name the class as Main. In Flash CS3 we define the Document Class as com.tracethis.Main. Inside of the Main class we can then target static methods within the Central class or instantiate Central and use any public members, all without having to specify an import for Central in Main.as.

    Ok. but what if there is an other class named Central in the package “com.tracethis” ? How can I reach the Central class in the default package from the Main class?

  10. Maxdisher | Wednesday, September 29th, 2010 | 5:14 AM

    Thanks for the tip, I just learned about this tag myself and ran into the same problem. This solved it!

Leave a Reply

If this is your first comment it will need to be approved before it will appear.


8 + three =