actionscript flex, how to send browser width to the swf itself
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 th开发者_C百科e browser resizes.
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>
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); }
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.
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.
精彩评论