是否可以在actionscript中使用for循环声明多个变量?

时间:2014-07-04 04:41:14

标签: actionscript-3 flash variables for-loop actionscript

我试图创建一个随机生成平台的平台游戏,但到目前为止,我必须逐个声明每个平台。这对我来说是一个问题,因为如果我想说,我的级别有100个楼层,我需要声明100个Sprite,然后是每个sprite的碰撞。除了直到现在我一直在做的方式之外,还有更简单的方法吗?

这就是我所拥有的:

    public var piso1:Sprite;
    public var piso2:Sprite;
    public var piso3:Sprite;
    public var piso4:Sprite;
    public var piso5:Sprite;
    public var piso6:Sprite;
    public var piso7:Sprite;
    public var piso8:Sprite;
    public var piso9:Sprite;
    public var piso10:Sprite;
    public var piso11:Sprite;
    public var piso12:Sprite;
    public var piso13:Sprite;
    public var piso14:Sprite;
    public var piso15:Sprite;
    public var piso16:Sprite;
    public var piso17:Sprite;
    public var piso18:Sprite;
    public var piso19:Sprite;
    public var piso20:Sprite;
    public var pisoganar:Sprite;

由于这些是精灵,我就是这样做的:

    public function cuadrados():void
    {
        for (var i:int =0; i<20; i++)
        {
            pisos[i] = dibujarCuadrado(0x000000, 100 + (Math.random() * (200 - 50 + 1) + 1), 25);       
        }

        piso2 = pisos[0]
        piso3 = pisos[1]
        piso4 = pisos[2]
        piso5 = pisos[3]
        piso6 = pisos[4]
        piso7 = pisos[5]
        piso8 = pisos[6]
        piso9 = pisos[7]
        piso10 = pisos[8]
        piso11 = pisos[9]
        piso12 = pisos[10]
        piso13 = pisos[11]
        piso14 = pisos[12]
        piso15 = pisos[13]
        piso16 = pisos[14]
        piso17 = pisos[15]
        piso18 = pisos[16]
        piso19 = pisos[17]
        piso20 = pisos[18]
    }

然后我逐一设定每个人的位置:

        piso1.x = stage.stageWidth /2;
        piso1.y = 600;

        piso2.x = stage.stageWidth /2;
        piso2.y = 500;

        piso3.x = piso2.x - (Math.random() * (400 - 100 + 1) + 100);
        piso3.y = 400;

        piso4.x = piso3.x + (Math.random() * (400 - 100 + 1) + 100);
        piso4.y = 300;

        piso5.x = piso4.x - (Math.random() * (400 - 100 + 1) + 100);
        piso5.y = 200;

        piso6.x = piso5.x + (Math.random() * (400 - 100 + 1) + 100);
        piso6.y = 100;

        piso7.x = piso6.x - (Math.random() * (400 - 100 + 1) + 100);
        piso7.y = 0;

        piso8.x = piso7.x + (Math.random() * (400 - 100 + 1) + 100);
        piso8.y = -100;

        piso9.x = piso8.x - (Math.random() * (400 - 100 + 1) + 100);
        piso9.y = -200;

        piso10.x = piso9.x + (Math.random() * (400 - 100 + 1) + 100);
        piso10.y = -300;

        piso11.x = piso10.x - (Math.random() * (400 - 100 + 1) + 100);
        piso11.y = -400;

        piso12.x = piso11.x + (Math.random() * (400 - 100 + 1) + 100);
        piso12.y = -500;

        piso13.x = piso12.x - (Math.random() * (400 - 100 + 1) + 100);
        piso13.y = -600;

        piso14.x = piso13.x + (Math.random() * (400 - 100 + 1) + 100);
        piso14.y = -700;

        piso15.x = piso14.x - (Math.random() * (400 - 100 + 1) + 100);
        piso15.y = -800;

        piso16.x = piso15.x + (Math.random() * (400 - 100 + 1) + 100);
        piso16.y = -900;

        piso17.x = piso16.x - (Math.random() * (400 - 100 + 1) + 100);
        piso17.y = -1000;

        piso18.x = piso17.x + (Math.random() * (400 - 100 + 1) + 100);
        piso18.y = -1100;

        piso19.x = piso18.x - (Math.random() * (400 - 100 + 1) + 100);
        piso19.y = -1200;

        piso20.x = piso19.x + (Math.random() * (400 - 100 + 1) + 100);
        piso20.y = -1300;

        pisoganar = dibujarCuadrado(0x00FF00,800, 25);
        pisoganar.x = stage.stageWidth / 2;
        pisoganar.y = -1400;

所以,是的。很多代码。有什么方法可以简化这个吗? (我原本是西班牙语,所以变量都是西班牙语。&#34; Piso&#34;意思是地板,&#34; cuadrados&#34;是方形的&#34; dibujarcuadrado&#34;是我的平局方形功能,基本上它创建,填充并放置在舞台上的方块)谢谢!

1 个答案:

答案 0 :(得分:0)

没有必要拥有这么多冗余代码。假设dibujarCuadrado返回Sprite,您可以执行此操作,而不是发布所有代码:

var tmpSprite:Sprite; //only need to define one

var tmpX:Number = stage.stageWidth /2; //store the current x
var tmpY:Number = 600; //store the starting y

var tmpRand:Number; //temporary variable to hold the random value every iteration

var pisos:Vector.<Sprite> = new Vector.<Sprite>(); //this array holds all your sprites

for (var i:int =0; i<20; i++)
{

    tmpSprite = dibujarCuadrado(0x000000, 100 + (Math.random() * (200 - 50 + 1) + 1), 25);  
    pisos.push(tmpSprite); //add to the array

    tmpRand = (Math.random() * (400 - 100 + 1) + 100);
    if(i % 2 == 0){ //the i % 2 is the remainder of i divided by 2, this make the condition alternate every iteration
        tmpSprite.x = tmpX - tmpRand;
    }else{
        tmpSprite.x = tmpX + tmpRand;
    }

    tmpX = tmpSprite.x;

    tmpSprite.y = tmpY - (100 * i);
}

这个代码甚至可以进一步减少(减少变量,内联条件)但我希望尽可能容易理解。