I want to put an array of objects into a grid. I know the code is simple but somehow I’m not getting what I want.
Code:
const PADDING:Number = 10:
const COL:Number = Math.floor(Math.sqrt(tiles.length);
const ROW:Number = Math.floor(Math.sqrt(tiles.length);
for(var i:int = 0; i< COL; i++)
{
var tile:TileSprite = tiles[i];
tile.x = i * tile.width + PADDING;
for(var j:int = 0; j < ROW; j++)
{
tile.y = j * tile.height + PADDING;
}
}
Notes:
– The array is declared else where as a public variable
– This is to be part of a game engine I’m building so I want it as flexible as possible
– The tiles as reading their properties for an XML file. But I don’t want to use the XML to hard code the tiles’ positions.
Thanks in advance for your help. Let me know if you need any more clarification.
I would suggest looking into the modulus operator rather than nesting loops.
This blog post outlines how to do just that:
http://www.davidpett.com/actionscript-3-dynamic-rows-and-columns/
Using David’s example as a starting point, your code would then look like this: