Setting the width and height of a pure-ActionScript application
For a Flex project with MXML files, it is obvious how to set the width and height of the project:
<mx:Application width="300" height="200">
But for pure-ActionScript projects, it isn't so obvious. The way to do it is to use the SWF metadata attribute above the declaration of the class:
package { [SWF(width="300", height="200")] public class MyApp extends Sprite { ... } }
The values specified there are pixel sizes; in this context, you are not allowed to use percentage values such as 100%.
Comments(22)
Thanks for the info!
Do you know where I could find documentation on other metadata one can use in a pure ActionScript project?
thanks!
http://livedocs.macromedia.com/labs/1/flex/00001437.html has a list of them; I think it is an oversight that [SWF] was not included there.
Note that you can also set FPS and bg color in the SWF meta tag
ex:
[SWF(width="300", height="200", frameRate="18", backgroundColor="#FFFFFF")]
You can also set the width, height, stage [background] color, and frame rate as a compiler argument.
Thanks for the tip!
[...] The background color of the application, as specified in the backgroundColor attribute of the <mx:Application> tag, or, in the case of ActionScript-only projects, in the backgroundColor field of the [SWF] metadata attribute of the main application class, e.g. [SWF(backgroundColor="#ffffff")] public class MyApp extends Sprite (see this post for more information on setting the width, height, and background color of an ActionScript project). The result is in the form #rrggbb, e.g. #ffffff. This can actually be a little tricky to use, because by default, the background of a Flex app is actually a gentle gradient from one color to another; if you want the HTML background to match the background of your Flex app, you may need to fiddle with both the backgroundColor and backgroundGradientColors attributes of the <mx:Application> tag. [...]
According to this blog entry by Derek Vadneau, the SWF metadata attribute should appear after any import statements.
http://blog.madebyderek.com/archives/2007/01/12/as3-projects-and-the-swf-metadata-tag/
That’s right, Adrian. Think of the SWF metadata tag as being connected to the class declaration, in the same way that the word "public" is connected to it. It’s also similar to other metadata tags such as [Bindable], [Style], [Event], etc. — it appears immediately before the element that it is modifying.
So I wonder how one can create a full-screen application in Pure AS3? Is there some way to determine the dimensions of the browser window in (or before) that Metadata tag? Thanks ;-)
PhoenixSol, just go ahead and make a Pure AS3 app; it will start out at 100% of the size of the browser (because the HTML will have <object> and <embed> tags with width=100% height=100%).
Oh, duh; Thanks Mike!
[...] mikemo » Setting the width and height of a pure-ActionScript application [...]
Thanks!
Wow. I searched for a while to figure out why my [swf] metadata wasn’t working! has to go on the line before the package declaration. Of course just like [bindable] before variable declarations. Wow that isn’t that obvious though, naughty Adobe, it isn’t in the O’Reilly book or the Flex2 training book.
how about width=100%??/ it says "no!"
Thanks Mike!
Metadata SWF attribute is hardcoded – what about programmatically set size? Something like stage.width=xxx,
sj314, I haven’t tried, but off the top of my head, I’m thinking the way to do that is to have some JavaScript code that changes the "width" and "height" attributes of the object and embed tags. So, I would probably use the Flex AJAX Bridge to communicate with JavaScript that is in the HTML that contains your SWF, and have some JavaScript that changes the width and height.
Awesome, thanks mike
[...] Setting the width and height of a pure-ActionScript application Posted June 27, 2006 [...]
Dont forget
[SWF(percentWidth="100", percentHeight='100', backgroundColor='#000000')]
[...] Mike Morearty [...]