Actionscript 3 按比例调整本机窗口的空气大小

Actionscript 3 按比例调整本机窗口的空气大小,actionscript-3,air,resize,window,Actionscript 3,Air,Resize,Window,我的应用程序启动时的舞台大小为1000 x 500,宽高比为2:1。本机窗口具有系统chrome,它将始终稍微高出几个像素 为了始终保持舞台的2:1高宽比,如何只允许本机窗口按比例调整大小 以下代码无法按预期工作: package { //Imports import flash.display.NativeWindow; import flash.display.Sprite; import flash.display.StageAlign; import flash.display.Stag

我的应用程序启动时的舞台大小为1000 x 500,宽高比为2:1。本机窗口具有系统chrome,它将始终稍微高出几个像素

为了始终保持舞台的2:1高宽比,如何只允许本机窗口按比例调整大小

以下代码无法按预期工作:

package
{
//Imports
import flash.display.NativeWindow;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.NativeWindowBoundsEvent;

//Class
[SWF(width="1000", height="500", frameRate="60", backgroundColor="#000000")]
public class WindowTest extends Sprite
    {
    //Constants
    private static const ASPECT_RATIO:Number = 2.0; //2:1 Aspect Ratio

    //Constructor
    public function WindowTest()
        {
        init();
        }

    //Initialization
    private function init():void
        {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZE, windowResizeEventHandler);
        }

    //Window Resize Event Handler
    private function windowResizeEventHandler(evt:NativeWindowBoundsEvent):void
        {
        evt.currentTarget.width = stage.stageHeight * ASPECT_RATIO;
        }
    }
}

阻止默认事件,并手动调整窗口大小:
编辑:看起来air以一种奇怪的方式计算宽度,所以为了防止在开始时闪烁,在SWF标记中将窗口大小设置为1050x500

package{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.NativeWindowBoundsEvent;

//Class
[SWF(width="1000", height="500", frameRate="60", backgroundColor="#000000")]
public class airtest extends Sprite
{
    //Constants
    private static const ASPECT_RATIO:Number = 2.0; //2:1 Aspect Ratio

    //Constructor
    public function airtest()
    {
        init();
    }

    //Initialization


private function init():void
    {
        stage.scaleMode = StageScaleMode.NO_SCALE;
        stage.align = StageAlign.TOP_LEFT;
        stage.nativeWindow.addEventListener(NativeWindowBoundsEvent.RESIZING, windowResizeEventHandler);
    }

    private function windowResizeEventHandler(evt:NativeWindowBoundsEvent):void
    {
        evt.preventDefault()
        if (evt.beforeBounds.width != evt.afterBounds.width){//user resizes width
            evt.currentTarget.width = evt.afterBounds.width
            evt.currentTarget.height = evt.afterBounds.width/ASPECT_RATIO;
        } else if (evt.beforeBounds.height != evt.afterBounds.height){
            evt.currentTarget.height = evt.afterBounds.height
            evt.currentTarget.width = evt.afterBounds.height*ASPECT_RATIO;
        }

    }
}
}

它对我有用。。您需要beta flex 4.5来编译Air actionscript项目。错误是什么?我正在将Flash Professional CS5与AIR 2.5 SDK一起使用。为什么我需要FlexSDK?没有错误。这个窗口根本就没有调整大小。嗯,可能是因为当我尝试它时,我将类命名为airtest?您将其命名为WindowTestno,我更改了类名和构造函数-否则它将无法编译。我正在用ADL运行你的代码。evt.preventDefault();导致事件不发生,并且不会触发后续代码,但会删除evt.preventDefault();导致窗口大小不成比例调整。在这两种情况下,似乎都忽略了后续代码。当我将程序作为AIR应用程序发布和运行时,也会发生同样的事情。我在Flex4.5测试版中编译了它,一切都很好。我只更改了windowResizeEventHandler函数,并将事件从NativeWindowBoundsEvent.RESIZE修改为NativeWindowBoundsEvent.Resizeing。代码应执行以下操作:如果检测到用户交互,但在窗口的实际参数更改之前,会发生NativeWindowBoundsEvent.RESIZING事件。我取消此事件,因为我想手动调整窗口大小。evt.beforeBounds包含用户交互后的原始窗口大小,evt.afterBounds包含窗口大小。