views:

275

answers:

4

I'm working with flex, but actionscript ideas are just as good.

The flex <s:Application> tag has height="100%" width="100%" so the swf fits the browser as the browser gets resized.

My problem is that I have a <s:Label> that I need to position based on the real/current size of the browser.

<s:Application height="100%" width="100%">
     .....
     <s:Label text="hello" x="?" y=">" />

</s:Application>

I heard it's possible to use Application.application.width; but I get a compile error, that it doesn't know what that is.

Any ideas how to do this. I'm trying to get the current size of the swf in the browser, as the browser resizes.

A: 

you could only know stage size when element is added inside stage, you could try a eventdelegate like this

app.addEventListener(Event.ADDED_TO_STAGE,init);
private function init(evt:Event):void{
   trace(stage.stageWidth);
   trace(stage.stageHeight);
}
markcial
That won't update on browser resize though, but only on the initial size.
Axelle Ziegler
There is also a RESIZE event, to be use like stage.addEventListener(Event.RESIZE, resizeHandler). http://www.adobe.com/devnet/flash/articles/liquid_gui_03.html
Lars
+2  A: 

As far as I can tell the following should work. Application.application simply is the same as this provided you are in the base application. The binding should allow the size to change after initialization.

<s:Application height="100%" width="100%">
     .....
     <s:Label text="hello" x="{width}" y="" />

</s:Application>

Edit : I just checked and it does work. To put your Label in the middle of the stage you simply have to put it like that

<s:Application height="100%" width="100%">
         .....
         <s:Label text="hello" x="{width/2}" y="{height/2}" />

</s:Application>
Axelle Ziegler
Seems it works in Flex3 too :)
Axelle Ziegler
I removed last comment. it's good in 3
adamcodes
A: 

Call a javascript function on browser resize. From there get the reference to the swf, and call the AS function to resize the label. Pass the browser width/height.

See example here

UPDATE: Correction: ExternalInterface goes AS to JS.

adamcodes
A: 

It's not completely clear what you are trying to do. x and y are positional elements, width and height are size elements. If you are positioning the label you should probably use top, left, right and bottom rather than x and y if you want it to size as the parent control sizes.

If you want the label centered you can use horizontalCenter and verticalCenter.

If you put it inside a layout control like a panel or a canvas you can just set its left and right to 0 and have the parent canvas size.

If your label is always in a different place depending on the size, you can override updateDisplayList to set the position. That holds whether you put it inside a layout control or not.

Simon