无法在AS3中将矩形缩放到指定比例

时间:2017-07-12 19:41:25

标签: actionscript-3

在过去的几个小时里,我一直在试图解决这个问题。我在里面创建了一个父Sprite gameBoard1和一个子sprite Block1。我使用点击功能来提升游戏水平。我想要做的是使gameBoard1的尺寸增加,这保持了Block1的尺寸。这可行,但当然,gameBoard1很快就不适合屏幕,所以我尝试缩放。我已经生成了比例因子:

var rescaleX:Number = resizeBoardX / gameBoard1Width; 

我想用这个数字来调整gameBoard1的比例:

gameBoard1.scaleX = rescaleX;
gameBoard1.scaleY = rescaleX;

最终发生的事情是gameBoard1(以及Block1)在每个"级别"上显得越来越小。由于我每次计算一个新级别时都会计算rescaleX值,因此gameBoard1应该显示为每个级别大致相同的大小,并且它应该只是看起来变小的子级(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);
}

1 个答案:

答案 0 :(得分:0)

请注意,使用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;
}