9 Replies Latest reply on Jan 14, 2008 7:52 AM by chrisatflash

# hot to create rows and coloms with attachMovie

Hello,
I am trying to read a number for example the number is :
var amount:Number = 10;

And what i would like to do now is make coloms 4 and attach a square on the stage.
So the result would be 10 squares with 4 coloms and 3 rows. (the last row has 2 squares)

like this:

X X X X
X X X X
X X

With the for loop i tried something but it fills out 4 times 4 = 16 squares.

Also is it possible to create an empty movieclip and attach these squares to it? Like an holder?

I attached an example.. (you have to create an movieclip and call it's linkage Id 'square' )

:

• ###### 2. Re: hot to create rows and coloms with attachMovie
With this loops u going very deep just you can use one loop and one if statement see and test example i creat i think is very easy more than the loops u try :)

var startX:Number = 0;
var startY:Number = 0;
var currentX:Number = startX;
var Width:Number = 20;
var Height:Number = 20;
var Row:Number=4;
for (var i:Number = 0; i<=10; i++) {
var mc = this.attachMovie("circle", "circle"+i, this.getNextHighestDepth());
mc._x = startX;
mc._y = startY;
if ((i+1)%Row == 0) {
startX = currentX;
startY += Height;
} else {
startX += Width;
}
}
• ###### 3. hot to create rows and coloms with attachMovie
Thanks! Both methods work great.
The code of Kglad is very clean and less of code.
The formule: 50*Math.floor(i*50/(50*colNum)) ... how do they make this up? very smart!

I agree to niki that i had too much loops going on.
Thanks for the input! But how can you attach these squares or circles to on movieclip?
Do you need to create an empty movieclip before or after the attach method?

• ###### 4. Re: hot to create rows and coloms with attachMovie
you're welcome.

the formula for the _y parameter can be deduced from your requirements: the first colNum i values should all result in the same _y value. the 2nd colNum i values should all result in the same _y value, 50 greater than the previous _y, etc.

so, that can be simplified:

miniHolder._y=50*Math.floor(i/colNum);
• ###### 5. Re: hot to create rows and coloms with attachMovie
Just you must hae a movieclip instans with some graphic into the library and afcource if you want new movieclip to store the new attached movieclips you must create one i use this :)
• ###### 6. Re: hot to create rows and coloms with attachMovie
I see you got it solved, but thought I'd pass along a little function I use
for this:

function gridLoc(index:Number, perRow:Number){
return [index % perRow, Math.ceil(index / perRow)];
}

You just pass it the index into the grid, from 1-n and how many items per
row and it returns an array containing [column, row] or x,y, as integers. So
trace(gridLoc(10,4)); will trace 2,3 - item 10 is at column 2, row 3 in a
grid with 4 items per row.

--
Dave -
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/

• ###### 7. hot to create rows and coloms with attachMovie
Nice one Dave, but should the value trace(gridLoc(4,4)); not be 4,1 in stead of 0,1 ?

I think there is a bug in this function..
I tried an example with your function, but as you see something goes wrong.

• ###### 8. Re: hot to create rows and coloms with attachMovie
Heh. Need to organize.

function gridLoc(index:Number, perRow:Number){
return [index % perRow == 0 ? perRow : index % perRow, Math.ceil(index /
perRow)];
}

--
Dave -
Head Developer
http://www.blurredistinction.com
Adobe Community Expert
http://www.adobe.com/communities/experts/

• ###### 9. Re: hot to create rows and coloms with attachMovie
nice one Dave!