# For loops and attachMovie

**URL:** <https://forum.kirupa.com/t/for-loops-and-attachmovie/184927>\
**Category:** Uncategorized\
**Created:** [April 9, 2006, 11:29am UTC](https://forum.kirupa.com/t/for-loops-and-attachmovie/184927 "2006-04-09T11:29:15Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)\
**Post date:** [April 9, 2006, 11:29am UTC](https://forum.kirupa.com/t/for-loops-and-attachmovie/184927/1 "2006-04-09T11:29:15Z")

</div>

Hi there,

I want to attach a MC dynamically so that I have 12 rows and 12 columns of \_mc box on the main stage:

```auto

xpos = 50;
ypos = 50;
for(i=0;i<12;i++){
 
 this.attachMovie("box","bx"+i,i,{_x:xpos,_y:ypos});
 xpos+= this["bx"+i]._width+5;
 
}

```

The code attaches the MC in only one row. when I add:

```auto

ypos+= this["bx"+i]._height+5;

```

I get nothing on the stage.  
I think it can be done only with a nested loop but so far, well, my head’s in a loop!

Any help much appreciated.  
Geminian1

---

<div class="post-metadata">

**Author:** ![devonair](https://avatars.discourse-cdn.com/v4/letter/d/3be4f8/32.png) [@devonair](https://forum.kirupa.com/u/devonair)\
**Post date:** [April 9, 2006, 2:21pm UTC](https://forum.kirupa.com/t/for-loops-and-attachmovie/184927/2 "2006-04-09T14:21:05Z")

</div>

you can try my Grid class that no one seems to like but me… 🙂

[AS] /\*\*  
Grid class  
@author: Devon O.  
@date: 15JAN06  
_/  
//  
//  
class Grid extends Array  
{  
/_\*  
\* Grid returns an array of x and y coordinates based on the number of columns, rows, height and width of cells,  
\* and the x and y value of the upper left corner.  
\*  
\* @usage myGrid=new Grid (columns, rows, cellHeight, cellWidth, topLeftCornerX, topLeftCornerY)  
\* @param cols: The number of columns  
\* @param rows: The number of rows  
\* @param cellHeight: The height of the grid cell  
\* @param cellWidth: The width of the grid cell  
\* @param x: The x position of the top left corner of the grid  
\* @param y: The y position of the top left corner of the grid  
\* @return Grid (an array of objects each containing two properties: x and y)  
_/  
public function Grid (cols : Number, rows : Number, cellHeight : Number, cellWidth : Number, x : Number, y : Number)  
{  
var numCells : Number = cols \* rows;  
var curx : Number = x;  
for (var i = 0; i \< numCells; i ++)  
{  
this \* = new Object (  
{  
x : curx, y : y  
});  
curx += cellWidth;  
if ( ! ((i + 1) % cols))  
{  
curx = x;  
y += cellHeight;  
}  
}  
}  
//  
/_\*  
\* The shuffle method will return the array of grid coordinates in a random order.  
\*  
\* @usage myGrid.shuffle()  
\* @return Void  
\*/  
public function shuffle () : Void  
{  
var len : Number = this.length;  
var temp : Object = new Object ();  
for (var i = 0; i \< len; i ++)  
{  
var rand : Number = Math.floor (Math.random () \* len);  
temp = this \*;  
this \* = this [rand];  
this [rand] = temp;  
}  
}  
}[/AS]

To use:

[AS]// pretend the “box” mc is 25 pixels by 25 pixels and you want 5 pixels between them -  
// that’s why the 30…  
var myGrid:Grid = new Grid(12, 12, 30, 30, 50, 50);  
var len:Number = myGrid.length;  
for (var i = 0; i \< len; i++) {  
var myBox:MovieClip = this.attachMovie(“box”, “box” + i, i, {\_x:myGrid\*.x, \_y:myGrid\*.y});  
}[/AS]

hope that helps out…

---

<div class="post-metadata">

**Author:** ![Geminian1](https://avatars.discourse-cdn.com/v4/letter/g/90db22/32.png) [@Geminian1](https://forum.kirupa.com/u/Geminian1)\
**Post date:** [April 9, 2006, 4:32pm UTC](https://forum.kirupa.com/t/for-loops-and-attachmovie/184927/3 "2006-04-09T16:32:01Z")

</div>

Hey Devonair,

Thanks for the help. It works beautifully but can it be done without having to write a class?

Geminian1

---

<div class="post-metadata">

**Author:** ![JoshuaJonah](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/joshuajonah/32/1775_2.png) [@JoshuaJonah](https://forum.kirupa.com/u/JoshuaJonah)\
**Post date:** [April 9, 2006, 4:40pm UTC](https://forum.kirupa.com/t/for-loops-and-attachmovie/184927/4 "2006-04-09T16:40:29Z")

</div>

yeah, but the class is much nicer for modularity(word?)
