Skip to main content
February 2, 2009
Question

Writing loops and positioning

  • February 2, 2009
  • 2 replies
  • 274 views
Hi,

I am writing a loop and want to position 25 boxes in a square. I can make 5 boxes appear in a line, but can't make 25 boxes appear in a square without writing 5 seperate loops (e.g. j, k, l, m, etc.). This seems long-handed, can I shorten the code? Here is the code I have used so far:

var box:Box;


for(var i:int = 0; i < 5; i++)
{
box = new Box();
addChild(box)
box.x = 100 + i * 40;
box.y = 100;
}
This topic has been closed for replies.

2 replies

February 2, 2009
Great, thanks very much.

I had thought of running a loop within a loop but used "i" for both the x and y values which did the same as running one loop 25 times. This has done exactly what I wanted.

Thanks again,
Mikey
February 2, 2009
Hi loki2357,

You need to add another loop for the y values inside your existing loop in order to position the y values as per the code below.

There will be 25 iterations in all. The first loop sets the columns and the second loop sets the rows. If you wanted more rows, just increase the number of iterations for the second for statement. If you want more or less columns, just change the first loop.

I haven't tested this, but it should work in theory.......

Regards

Paul