无法在 AS3 中将矩形缩放到指定比例
Trouble scaling rectangle to specified scale in AS3
过去几个小时我一直在摸不着头脑,试图解决这个问题。我在里面创建了一个 parent Sprite gameBoard1 和一个 child sprite Block1。我正在使用点击功能来提升游戏水平。我想要做的是增加 gameBoard1 的尺寸,从而保持 Block1 的尺寸。这行得通,但当然,gameBoard1 很快就不适合屏幕了,所以我正在尝试缩放。我已经生成了比例因子:
var rescaleX:Number = resizeBoardX / gameBoard1Width;
我想用那个数字来调整 gameBoard1 的比例:
gameBoard1.scaleX = rescaleX;
gameBoard1.scaleY = rescaleX;
最终发生的是 gameBoard1(连同 Block1)在每个 "level" 上都显得越来越小 "level" 因为每次有新关卡时我都在计算 rescaleX 值,gameBoard1 应该显示大致相同每个级别的大小,只是 children (Block1) 应该看起来变小,因为 gameBoard1 的实际大小。
这是我的第一款游戏,我只是在摸索,但这让我一头雾水。
var YtoXratio:Number = stage.stageHeight / stage.stageWidth;
var resizeBoardX:Number = stage.stageWidth * 0.95;
var resizeBoardY:Number = stage.stageHeight * 0.95;
trace ("resizeBoardX and Y are 95% of the stage width/height " + resizeBoardX + " x " + resizeBoardY);
var Xcenter:Number = stage.stageWidth / 2 - resizeBoardX / 2;
var Ycenter:Number = stage.stageHeight / 2 - resizeBoardY / 2;
var gameBoard1:Sprite = new Sprite;
gameBoard1.graphics.beginFill(0xFF0000);
gameBoard1.graphics.drawRect(0, 0, stage.stageWidth , stage.stageHeight);
//gameBoard1.graphics.endFill();
addChild(gameBoard1);
gameBoard1.x = Xcenter;
gameBoard1.y = Ycenter;
gameBoard1.scaleX = 0.95;
gameBoard1.scaleY = gameBoard1.scaleX;
var block1:Shape = new Shape;
block1.graphics.beginFill(0x0000FF);
block1.graphics.drawRect(0, 0, 400, 400);
block1.graphics.endFill();
gameBoard1.addChild(block1);
var gameBoard1Width:Number = gameBoard1.width;
var gameBoard1Height:Number = gameBoard1Width * YtoXratio;
gameBoard1.addEventListener(MouseEvent.CLICK, nextLevel);
function nextLevel (e:MouseEvent):void{
gameBoard1Width = gameBoard1Width + 100;
gameBoard1Height = gameBoard1Width * YtoXratio;
gameBoard1.width = gameBoard1Width;
gameBoard1.height = gameBoard1Height;
var rescaleX:Number = resizeBoardX / gameBoard1Width;
trace("the scale ratio should be " + rescaleX);
gameBoard1.scaleX = rescaleX;
gameBoard1.scaleY = rescaleX;
trace("gameBoard1Width =" + gameBoard1Width + " gameBoard1Height =" + gameBoard1Height);
trace("the scaled gameboard should be " + gameBoard1Width * rescaleX + " x " + gameBoard1Height);
trace("the scaled gameBoard is " + gameBoard1.width + " x " + gameBoard1.height);
}
请注意,使用 stage.stageWidth
(和高度)是一个动态值,会随着内容的变化而变化(与使用任何其他 MovieClip
相同)。如果你想要一个静态值,要么在第一次加载时存储它,要么使用存储在 this.loaderInfo.width
(和高度)中的编译值。
以下代码将创建您的图板,并将内容调整为背景定义的预先计算的尺寸。每次您向板上添加另一个块时,板将调整大小以匹配背景。
var bg:Shape; // the visible background of our game board.
var board:Sprite; // the container for our blocks, which we'll rescale
addEventListener(Event.ENTER_FRAME, init);
function init(e:Event):void {
// Make sure we have a populated loader by referencing one of its properties.
var ready:Boolean;
try { ready = (this.loaderInfo.width > 0) ? true : false; } catch (e:Error) {}
if (ready) {
removeEventListener(Event.ENTER_FRAME, init); // Remove further frame tests.
// Create the Background
bg = new Shape();
bg.graphics.beginFill(0xFF0000);
bg.graphics.drawRect(0, 0, this.loaderInfo.width * 0.9, this.loaderInfo.height * 0.9);
bg.graphics.endFill();
bg.x = this.loaderInfo.width / 2 - bg.width / 2;
bg.y = this.loaderInfo.height / 2 - bg.height / 2;
addChild(bg);
// Create the board
board = new Sprite();
board.x = bg.x;
board.y = bg.y;
addChild(board);
board.addChild(createBlock());
// Create a button to advance the level
var txt:TextField = new TextField();
txt.text = "Next Level";
addChild(txt);
txt.addEventListener("click", nextLevel);
}
}
function nextLevel(e:Event):void {
// Create a new block
var block:Shape = createBlock();
board.addChild(block);
block.x = block.width * (board.numChildren - 1);
trace("Now on level " + board.numChildren);
// Resize the board to fit the background.
if (board.width > bg.width) {
board.width = bg.width;
board.scaleY = board.scaleX;
}
}
function createBlock():Shape {
var block:Shape = new Shape();
block.graphics.beginFill(Math.floor(Math.random() * (1+0xFFFFFF-0x000000)) + 0x000000);
block.graphics.drawRect(0,0,200,200);
block.graphics.endFill();
return block;
}
过去几个小时我一直在摸不着头脑,试图解决这个问题。我在里面创建了一个 parent Sprite gameBoard1 和一个 child sprite Block1。我正在使用点击功能来提升游戏水平。我想要做的是增加 gameBoard1 的尺寸,从而保持 Block1 的尺寸。这行得通,但当然,gameBoard1 很快就不适合屏幕了,所以我正在尝试缩放。我已经生成了比例因子:
var rescaleX:Number = resizeBoardX / gameBoard1Width;
我想用那个数字来调整 gameBoard1 的比例:
gameBoard1.scaleX = rescaleX;
gameBoard1.scaleY = rescaleX;
最终发生的是 gameBoard1(连同 Block1)在每个 "level" 上都显得越来越小 "level" 因为每次有新关卡时我都在计算 rescaleX 值,gameBoard1 应该显示大致相同每个级别的大小,只是 children (Block1) 应该看起来变小,因为 gameBoard1 的实际大小。
这是我的第一款游戏,我只是在摸索,但这让我一头雾水。
var YtoXratio:Number = stage.stageHeight / stage.stageWidth;
var resizeBoardX:Number = stage.stageWidth * 0.95;
var resizeBoardY:Number = stage.stageHeight * 0.95;
trace ("resizeBoardX and Y are 95% of the stage width/height " + resizeBoardX + " x " + resizeBoardY);
var Xcenter:Number = stage.stageWidth / 2 - resizeBoardX / 2;
var Ycenter:Number = stage.stageHeight / 2 - resizeBoardY / 2;
var gameBoard1:Sprite = new Sprite;
gameBoard1.graphics.beginFill(0xFF0000);
gameBoard1.graphics.drawRect(0, 0, stage.stageWidth , stage.stageHeight);
//gameBoard1.graphics.endFill();
addChild(gameBoard1);
gameBoard1.x = Xcenter;
gameBoard1.y = Ycenter;
gameBoard1.scaleX = 0.95;
gameBoard1.scaleY = gameBoard1.scaleX;
var block1:Shape = new Shape;
block1.graphics.beginFill(0x0000FF);
block1.graphics.drawRect(0, 0, 400, 400);
block1.graphics.endFill();
gameBoard1.addChild(block1);
var gameBoard1Width:Number = gameBoard1.width;
var gameBoard1Height:Number = gameBoard1Width * YtoXratio;
gameBoard1.addEventListener(MouseEvent.CLICK, nextLevel);
function nextLevel (e:MouseEvent):void{
gameBoard1Width = gameBoard1Width + 100;
gameBoard1Height = gameBoard1Width * YtoXratio;
gameBoard1.width = gameBoard1Width;
gameBoard1.height = gameBoard1Height;
var rescaleX:Number = resizeBoardX / gameBoard1Width;
trace("the scale ratio should be " + rescaleX);
gameBoard1.scaleX = rescaleX;
gameBoard1.scaleY = rescaleX;
trace("gameBoard1Width =" + gameBoard1Width + " gameBoard1Height =" + gameBoard1Height);
trace("the scaled gameboard should be " + gameBoard1Width * rescaleX + " x " + gameBoard1Height);
trace("the scaled gameBoard is " + gameBoard1.width + " x " + gameBoard1.height);
}
请注意,使用 stage.stageWidth
(和高度)是一个动态值,会随着内容的变化而变化(与使用任何其他 MovieClip
相同)。如果你想要一个静态值,要么在第一次加载时存储它,要么使用存储在 this.loaderInfo.width
(和高度)中的编译值。
以下代码将创建您的图板,并将内容调整为背景定义的预先计算的尺寸。每次您向板上添加另一个块时,板将调整大小以匹配背景。
var bg:Shape; // the visible background of our game board.
var board:Sprite; // the container for our blocks, which we'll rescale
addEventListener(Event.ENTER_FRAME, init);
function init(e:Event):void {
// Make sure we have a populated loader by referencing one of its properties.
var ready:Boolean;
try { ready = (this.loaderInfo.width > 0) ? true : false; } catch (e:Error) {}
if (ready) {
removeEventListener(Event.ENTER_FRAME, init); // Remove further frame tests.
// Create the Background
bg = new Shape();
bg.graphics.beginFill(0xFF0000);
bg.graphics.drawRect(0, 0, this.loaderInfo.width * 0.9, this.loaderInfo.height * 0.9);
bg.graphics.endFill();
bg.x = this.loaderInfo.width / 2 - bg.width / 2;
bg.y = this.loaderInfo.height / 2 - bg.height / 2;
addChild(bg);
// Create the board
board = new Sprite();
board.x = bg.x;
board.y = bg.y;
addChild(board);
board.addChild(createBlock());
// Create a button to advance the level
var txt:TextField = new TextField();
txt.text = "Next Level";
addChild(txt);
txt.addEventListener("click", nextLevel);
}
}
function nextLevel(e:Event):void {
// Create a new block
var block:Shape = createBlock();
board.addChild(block);
block.x = block.width * (board.numChildren - 1);
trace("Now on level " + board.numChildren);
// Resize the board to fit the background.
if (board.width > bg.width) {
board.width = bg.width;
board.scaleY = board.scaleX;
}
}
function createBlock():Shape {
var block:Shape = new Shape();
block.graphics.beginFill(Math.floor(Math.random() * (1+0xFFFFFF-0x000000)) + 0x000000);
block.graphics.drawRect(0,0,200,200);
block.graphics.endFill();
return block;
}