这是我创建的一段基本代码,它生成一个随机的双色关卡:
var map:Array = new Array();
var mLength:int = 28;
var mHeight:int = 16;
var mArea:int = mLength * mHeight;
var tileWidth:int = 20;
var tileHeight:int = 20;
var tileX:int = 0 - tileWidth;
var tileY:int = 0;
var genTile:int;
var genDone:Boolean = false;
var waterChance:int = 10;
var grassChance:int = 33;
var tile:Sprite = new Sprite();
var waterTile:Sprite = new Sprite();
var waterHolder:Sprite = new Sprite();
var genericTileHolder:Sprite = new Sprite();
var hitting:Boolean = false;
var i:int = 0;
for (i = 0; i < mArea; i++) {
if (map[i - 27] == 1) {
waterChance * 8;
grassChance / 2;
}
if (map[i - 28] == 1) {
waterChance * 8;
grassChance / 2;
}
if (map[i - 29] == 1) {
waterChance * 8;
grassChance / 2;
}
if (map[i - 1] == 1) {
waterChance * 8;
grassChance / 2;
}
tileX += tileWidth;
if (tileX >= mLength * tileWidth) {
tileX = 0;
tileY += tileHeight;
}
genTile = (Math.round((Math.random()*(waterChance+grassChance))));
if (0 < genTile < waterChance) {
waterTile.graphics.beginFill(0x0033CC);
waterTile.graphics.drawRect(tileX,tileY,tileWidth,tileHeight);
waterHolder.addChildAt(waterTile, 0);
map.push("1");
}
if ((waterChance + 1) < genTile && genTile < (waterChance + grassChance)) {
tile.graphics.beginFill(0x216B18);
tile.graphics.drawRect(tileX,tileY,tileWidth,tileHeight);
genericTileHolder.addChildAt(tile, 0);
map.push("2");
}
grassChance = 33;
waterChance = 10;
}
stage.addChildAt(waterHolder, 0);
stage.addChildAt(genericTileHolder, 1);
问题有两个方面。一,每当数组生成时,长度似乎有一个随机的变化——使用 trace(map.length) 我得到了很多不同的长度,从 750 到 780,而它最多只能是 400。
其次,每当我跟踪关卡本身时,使用 trace(map) 我发现第一个元素似乎默认设置为 1。我无法弄清楚它为什么这样做,因为它应该在 20 之后至少一次为 2尝试。