0

This is a basic piece of code I've created that generates a random two-coloured level:

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

The problem is two-fold. One, whenever the array generates, the length seems to have a random bit of variation- using trace(map.length) I get a lot of different lengths, from 750 to 780, when it should only be 400 at most.

Secondly, whenever I trace the level itself, using trace(map) I find that the first element seems to be set by default to 1. I can't figure out why it's doing this, as it should be 2 at least once after 20 tries.

2 Answers 2

1

You cant compare more than 2 items in actionscript:

if (0 < genTile < waterChance) {...

It will always return true (i think). Instead:

if ((0 < genTile) && (genTile < waterChance)) {...

Also, looks like each loop can do map.push("1") and map.push("2") - hence map is larger than mArea - i assume it should do one or the other?

Sign up to request clarification or add additional context in comments.

2 Comments

It will actually get evaluated as (0 < genTile) < waterChance, which is always true here but not generally. His parameters should prevent both being pushed on the same loop (once correctly tested), but an if...else would be cleaner and safer.
That's not the problem as far as I can tell, it generates the level (image-wise) perfectly. I'll change it later when I have access to the code and check, however.
0

Problem 2: first element of map is 1 by default

if (0 < genTile < waterChance)
{
    waterTile.graphics.beginFill (0x0033CC); // Blue
    waterTile.graphics.drawRect (tileX,tileY,tileWidth,tileHeight);
    waterHolder.addChildAt (waterTile, 0);
    map.push ("1");
}

Since < operator is left-associative, the expression will be evaluated as (0 < genTile) < waterChance.

Since genTile is never less than 0, (0 < genTile) is always false.

And since waterChance is also never less than 0, (false < waterChance)

which translates to (0 < waterChance) is always true.

That makes (0 < genTile) < waterChance always true.

Therefore the first element of map is always "1".

Problem 1: map.length varies

if ((waterChance + 1) < genTile && genTile < (waterChance + grassChance))
{
    tile.graphics.beginFill (0x216B18); // Green
    tile.graphics.drawRect (tileX,tileY,tileWidth,tileHeight);
    genericTileHolder.addChildAt (tile, 0);
    map.push ("2");
}

Since mArea equals 448, the water tiles will always total to 448 .

And since genTile is a random number, the grass tiles will always total to a random number.

Therefore map.length varies.

I hope that helps.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.