0

This code is meant to place an instance of a movieclip called BasicGrid on to my stage 25 times, then if the X co-ordinate of the new movieclip is over 128 it starts placing them on a new line.

But for some reason I can't get it to work no matter what I try, all it does is place 25 squares in the exact same position, any advice/help would be appreciated, thanks!

for (var i:uint; i <= 25; i++) {
    var newOpenGrid:BasicGrid = new BasicGrid();
    newOpenGrid.x + 32;
    if(newOpenGrid.x > 128) {
        newOpenGrid.x = 32;
        newOpenGrid.y += 32;
    }
    addChild(newOpenGrid);
    trace(this.numChildren);
    trace("X: " + newOpenGrid.x);
    trace("Y: " + newOpenGrid.y);
    trace("i: " + i);
}
koopajah
  • 23,792
  • 9
  • 78
  • 104
CodeMode
  • 55
  • 2
  • 8

2 Answers2

1

You could also use a nested for loop:

var ySpace:uint = 32;
var xSpace:uint = 32;
var yStart:uint = 32;
var xStart:uint = 32;
for(var row:uint = 0; row < 5; row++) {
    for(var col:uint = 0; col < 5; col++) {
        var newOpenGrid:BasicGrid = new BasicGrid();
        newOpenGrid.x = xStart + col * xSpace;
        newOpenGrid.y = yStart + row * ySpace;
        addChild(newOpenGrid);
    }
}
bwroga
  • 5,379
  • 2
  • 23
  • 25
0

Solved it, I used variable that took the old X and Y value of the squares which lets the new square know where to place itself and if it need to start a new line, here is the revised code.

for (var i:uint; i <= 24; i++) {

    var newOpenGrid:BasicGrid = new BasicGrid();
    var gridX:int;
    var gridY:int;
    newOpenGrid.x = gridX;
    newOpenGrid.x += 32;
    newOpenGrid.y = gridY;
    if(newOpenGrid.x > 160) {
        newOpenGrid.x = 32;
        newOpenGrid.y += 32;
    }
    addChild(newOpenGrid);
    gridX = newOpenGrid.x;
    gridY = newOpenGrid.y;
    trace(this.numChildren);
    trace("X: " + newOpenGrid.x);
    trace("Y: " + newOpenGrid.y);
    trace("i: " + i);
    }
}
CodeMode
  • 55
  • 2
  • 8