1

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

1 Answer 1

3

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);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thats fantastic. Youve saved me a lot of time

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.