atmd
atmd

Reputation: 7490

Flash loop - Actionscript 3 grid loop

I am trying to create a grid for a flash game to use with a pathfinder class(will build once this is done)

I have the code

var rows:int = 4;
var cols:int = 4;
for (var py:int = 0; py <rows; py++) {
    for (var px:int = 0; px <cols; px++) {
        var box:Box = new Box();
        box.x = 50 + box.width * px;
        box.y = 50 + box.height * py;
        addChild(box);
    }
}

this adds a movieclip to the stage for the number of needed cols and rows, however, I need the grid to be: 17x21 with the cell sizes to be 20px however every other cell needs to be 36px ie. . .

| 36px | 20px | 36px | 20px | 36px | 20px | (all at 20px height)
| 36px | 20px | 36px | 20px | 36px | 20px | (all at 36px height)
| 36px | 20px | 36px | 20px | 36px | 20px | (all at 20px height)
| 36px | 20px | 36px | 20px | 36px | 20px | (all at 36px height)

Anyone have any ideas?

cheers

Andrew

Upvotes: 1

Views: 377

Answers (1)

laurent
laurent

Reputation: 90776

If I understand correctly, you need to check the modulo of px and py and set the width and height according to it. Something like that should work:

var rows:int = 4;
var cols:int = 4;
for (var py:int = 0; py <rows; py++) {
    for (var px:int = 0; px <cols; px++) {
        var box:Box = new Box();
        box.x = 50 + box.width * px;
        box.y = 50 + box.height * py;
        if (px % 2 == 0) {
            box.width = 36;
        } else {
            box.width = 20;
        }

        if (py % 2 == 0) {
            box.height = 20;
        } else {
            box.height = 36;
        }
        addChild(box);
    }
}

Upvotes: 3

Related Questions