2011-02-14 53 views
0

我正在尝试为Flex应用程序创建一个动态布局。如何根据舞台宽度定义布尔值?

我需要一个布尔值,它是依赖于浏览器窗口的总宽度与设置状态,沿着

if(this.parentApplication.width<950) 
     { 
      currentState = "wide" 
     }else{ 
      currentState = "narrow" 
     } 

不过线的东西,我不能只说明这一点在外汇:脚本标签,那么实现它的最佳方式是什么? enterFrame="application1_enterFrameHandler(event)"的作品,但我不禁感到,这可能是不必要的,可怕的计算密集型(我的应用程序运行速度足够慢,预计在不久的将来会出现一两个与flex效率有关的问题)。

感谢

乔希

回答

1

确实在等待调整尺寸会更便宜,更优雅。 in as3 S tage类不再存在,被替换为s tage。 语法也许不起作用(as3不喜欢匿名函数:))。

你需要在舞台上可用(check this例如),然后把 类似:

[...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...] 

然后

private function onResizeHandler(e:Event):void 
{ 
    currentState = (stage.stageWidth < 950) ? 'wide' : 'narrow'; 
    //or testing a given object's width rather than the stage. 
} 
0

你可以尝试使用一个事件处理程序。

myListener = new Object(); 
myListener.onResize = function() { 
    currentState = (obj.parentApplication.width<950) ? 'wide' : 'narrow'; 
}; 
Stage.addListener(myListener); 

对不起我的ActionScript是有点生疏,它已经有一段时间,但你可能不得不改变obj.parentApp...,据我所知,在这里使用this将指向一般是通过传递的参数回调函数。

编辑:忘了提到会发生什么..基本上,当用户调整电影舞台时,会触发匿名函数..比检查每个帧事件要便宜很多。