2010-12-02 140 views
2

我使用x/y座標將對象放置在屏幕上。我想要一種方法來根據屏幕寬度/高度的更改進行更新。所以如果使用重新調整大小的瀏覽器窗口x/y應該改變。每次屏幕重新調整大小時,我如何才能啓動一項功能。如何檢測Flex中的屏幕寬度/高度變化

回答

3

你可以做到這一點通過附加的ResizeEvent監聽器,你要跟蹤的任何對象:

http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/mx/events/ResizeEvent.html

public class Main extends Sprite { 
    public function Main () { 

     stage.addEventListener(Event.RESIZE, resizeListener); 
    } 

    private function resizeListener (e:Event):void { 
     trace("The application window changed size!"); 
     trace("New width: " + stage.stageWidth); 
     trace("New height: " + stage.stageHeight); 
    } 
    } 
1

你可以設置一個監聽器上在MXML中Application標籤右邊的Resize事件。

儘管您可能想要探索基於百分比的設置,但在大多數情況下,這樣做的效果會更差。

1

檢測屏幕分辨率的變化,通過簡單地添加在應用程序級別事件偵聽器,並使用事件屬性探測變化


private function init():void 
{ 
    this.addEventListener(ResizeEvent.RESIZE, resizeApplication); 
} 

public function resizeApplication(event:ResizeEvent):void 
{ 
    if(this.stage.stageHeight != event.oldHeight) 
    { 
     //perform action here or just use the above properties in any way you want 
    } 

} 

我做了這個功能公開,以便它可以在任何地方使用的最佳途徑在應用程序中,如果事件被解僱但附加到它的視圖沒有收聽。

相關問題