地形生成AS3

时间:2012-10-23 17:18:19

标签: actionscript-3 actionscript

我试图通过在舞台上放置一些随机方块来做地形,但我不能这样做,这是我的代码。

import flash.display.Sprite;


var i:Number;
var h:Number = 0;
var black:Sprite = new Sprite;


for (i = 0; i < 600; i++)
{
    if (randon <= 0.5)
    {
        i += 40;
        black.graphics.beginFill(000000);
        black.graphics.drawRect(i,h,40,40);
        black.graphics.endFill();
        addChild(black);
    }

    if (randon>=0.5)
    {
        i +=  40;
    }
}


var randon:Number;
randon = Math.random();

1 个答案:

答案 0 :(得分:3)

import flash.display.Sprite;

var h:Number=0;
var black:Sprite = new Sprite() ; // missing constructor
addChild(black);

for (var i:int=0; i<600; i+=40)  // can do definition in here - int is preferred
{
    if (Math.random()<=0.5) // inline
    {
        black.graphics.beginFill(000000);
        black.graphics.drawRect(i,h,40,40);
        black.graphics.endFill();
    }
}