ActionScript 3 - 通过浏览器动态调整MovieClip的大小但保留PROPORTIONS

时间:2012-07-10 23:28:28

标签: actionscript-3 dynamic resize movieclip

我有一个尺寸为500x400的影片剪辑。实例名称为:“main”。我有以下代码:

import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener (Event.RESIZE, resizeHandler);

// initialize sizing
resizeHandler (null);

function resizeHandler (event:Event):void {
var sw:Number = stage.stageWidth;
var sh:Number = stage.stageHeight;

  main.height = sh;
  main.width = sw;

}

如何将MovieClip的调整大小和最大大小保持为500x400?

3 个答案:

答案 0 :(得分:0)

这段代码可以完成这项工作:

if (stage.stageWidth/500 < stage.stageHeight/400)
{
    main.scaleX = stage.stageWidth/500;
    main.scaleY = main.scaleX;
}else{
    main.scaleX = stage.stageHeight/400;
    main.scaleY = main.scaleX;
}

答案 1 :(得分:0)

这不是我的头脑,所以不是100%它是正确的语法,但你应该明白:

import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;

var ratio:Number = Number(stage.stageWidth)/stage.stageWidth;//you need to check if stage isn't null and this ratio is valid

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener (Event.RESIZE, resizeHandler);


// initialize sizing
resizeHandler (null);

function resizeHandler (event:Event):void {
var sw:Number = stage.stageWidth;
var sh:Number = stage.stageHeight;
  //make with width same as stage
  main.width = sw;
  main.height = sh/ratio;//keep height proprtional
  //or you can try the other way around if you want - make main.height = sh and main.width =  sh * ratio;

}

答案 2 :(得分:0)

无论影片剪辑的比例如何,以下代码都可以实现。如果您希望强制执行500比400的比例,则可以将movieClipRatio指定为500/400。

import flash.display.StageScaleMode;
import flash.display.StageAlign;
import flash.events.Event;

stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;
stage.addEventListener (Event.RESIZE, resizeHandler);
var movieClipRatio:Number = main.width / main.height;

// initialize sizing
resizeHandler (null);

function resizeHandler (event:Event):void {
    var sw:Number = stage.stageWidth;
    var sh:Number = stage.stageHeight;

    if ( sw == 0 || sh == 0 ) {
        return; // special case to avoid
    }

    var stageRatio:Number = sw / sh;

    if ( stageRatio > movieClipRatio )  {
        // stage is proportionally wider than movieClip
        // resize by height
        main.width *= sh / main.height;
        main.height = sh;
    } else {
        // stage is proportionally taller than movieClip
        // resize by width
        main.height *= sw / main.width;
        main.width = sw;
    }

}