How can I define a boolean based on stage width?
I'm trying to create a dynamic layout for a flex app.
I need a boolean which is dependent upon the overall width of the browser window with which to set states, something along the lines of
if(this.parentApplication.width<950)
{
currentState = "wide"
}else{
currentState = "narrow"
}
However, I can't just state this in the fx:Script tags, so what is the best way to implement this? enterFrame="application1_enterFrameHandler(event)"
works, but I can't help but feel like this is probably unnecessary and horribly computation intensive (and my app开发者_C百科lication runs slow enough as it is, expect a question or two relating to flex efficiencies in the near future).
Thanks
Josh
indeed waiting for a resize will be cheaper and more elegant. in as3 the Stage class doesn't exist anymore and is replaced by stage. the syntax might not work either (as3 doesn't like anonymous functions very much :) ).
you'll need the stage to be available (check this for instance) and then put something like:
[...when stage is available...]
stage.addEventListener( Event.RESIZE, onResizeHandler );
onResizeHandler( null );//calling the method with an null event to force a first check
[...other instructions...]
and then
private function onResizeHandler( e:Event ):void
{
currentState = ( stage.stageWidth < 950 ) ? 'wide' : 'narrow';
//or testing a given object's width rather than the stage.
}
You could try using an event handler.
myListener = new Object();
myListener.onResize = function() {
currentState = (obj.parentApplication.width<950) ? 'wide' : 'narrow';
};
Stage.addListener(myListener);
Sorry my ActionScript is a little rusty, it's been a while, but you'll probably have to change the obj.parentApp...
, as far as I know, using this
in here would refer to the arguments usually passed through the callback function.
Edit: Forgot to mention what happens.. basically the anonymous function gets triggered when the movie stage is resized by the user.. a lot less expensive than checking every frame event.
精彩评论