PDA

View Full Version : Is there any grid component in AS 3.0


anujsharma
03-11-2008, 01:09 AM
Hi All
I am looking for a grid container in AS 3.0. With my search I found that AS 3.0 supports Data Grid but for holding data not for visual layout perspective. I am looking something for visual layout perspective. My aim is that if user drops 4 objects on the main screen and hits arrange button then 4 objects will be arranged in a 2x2 grid format. Any ideas e(xcept to manually specifying the coordinates) how would i do that and which container i will use for that.
Thanks in advance
Anuj

lordofduct
03-11-2008, 02:25 AM
not that I know of...

you can use a for loop to do it though.


var horSpace:Number = //horizontal width of each tile in the grid
var verSpace:Number = //vertical height of each tile in the grid

var rows:int = //number of rows
var cols:int = //number of columns

if (rows * cols < myArray.length) {
trace("won't fit in grid");
return;
}

for (var i:int = 0; i != myArray.length; i++) {
var row:int = Math.floor(i / cols);
var col:int = i - row * cols;


myArray[i].x = col * horSpace;
myArray[i].y = row * verSpace;
}

Keep in mind, this numbers the rows and columns from 0 to n-1. not 1 to n.

you can make your own container yourself to do this.

Of course this could be rewritten to fit any number of rows with a fixed number of columns, OR any number of columns with a fixed number of rows.

for instance, fixed number of columns, replace rows with:
var rows:int = Math.floor(myArray.length / cols);
rows++;


[EDIT]
I'm so bored... ugh... anyways... here:

//String fixed defines if 'n' restricts "row" or "column", defaults to column.
function sortArrayIntoGrid(ar:Array, //array to sort
n:int, //bounding for row or column
hs:Number = 100, //horizontal spacing
vs:Number = 100, //vertical spacing
fixed:String = "column" //"row" for row bounding, "column" for column bounding
):void {
var rows:int;
var cols:int;

if (fixed != "column" || fixed != "row") {
trace ("FAILED: " + fixed + " is not a parameter of this function.");
return;
}

switch (fixed) {
case "row":
rows = n;
cols = Math.floor(ar.length / n) + 1;
break;
case "column":
cols = n;
rows = Math.floor(ar.length / n) + 1;
break;
}

for (var i:int = 0; i != ar.length; i++) {
var row:int = Math.floor(i / cols);
var col:int = i - row * cols;

ar[i].x = col * hs;
ar[i].y = row * vs;
}
}