Actionscript 3 在AS3中将矩形缩放到指定比例时出现问题

Actionscript 3 在AS3中将矩形缩放到指定比例时出现问题,actionscript-3,Actionscript 3,在过去的几个小时里,我一直在挠头想办法解决这个问题。我在里面创建了一个父精灵游戏板1和一个子精灵区块1。我正在使用点击功能来提升游戏的水平。我想做的是增加gameBoard1的尺寸,这将保持Block1的尺寸。这是可行的,但当然,gameBoard1很快就无法在屏幕上显示,所以我正在尝试缩放。我已经生成了我的比例因子: var rescaleX:Number = resizeBoardX / gameBoard1Width; 我想用这个数字来调整gameBoard1的比例: gameBoar

在过去的几个小时里,我一直在挠头想办法解决这个问题。我在里面创建了一个父精灵游戏板1和一个子精灵区块1。我正在使用点击功能来提升游戏的水平。我想做的是增加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);
}

请注意,使用
stage.stageWidth
(和高度)是一个动态值,随着内容的变化而变化(与使用任何其他
MovieClip
相同)。如果需要静态值,请在首次加载时存储它,或者使用编译后的值,这些值存储在(和高度)中

下面的代码将创建您的电路板,并将内容调整为背景定义的预先计算的尺寸。每次向电路板添加另一个块时,电路板将调整大小以匹配背景

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;
}

如果您不希望
block1
gameBoard
一起缩放/移动,那么它为什么是
gameBoard
的子级呢?我想知道gameBoard的用途是什么,它看起来只是一个红色背景?最终,gameBoard上会有很多子对象。gameBoard的红色背景只是一个占位符atm,它最终将只是一个透明的矩形,填充有子对象(碎片),随着gameBoard的缩放,gameBoard上的所有碎片也将如此。我得到的是在更高的级别上调整、缩放和平移游戏板的能力,因为游戏板太大,无法在屏幕上显示。嗯,有趣的是,我必须多玩一点。我不想在添加一个新的方块时调整棋盘的大小,但不管有多少方块,整个棋盘都要在玩家控制下调整大小。我想这里有足够的信息让我有所进步,谢谢!