# addChild in sequence?

**URL:** <https://forum.kirupa.com/t/addchild-in-sequence/263147>\
**Category:** flash\
**Created:** [June 12, 2008, 10:00pm UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147 "2008-06-12T22:00:54Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![bennymartensson](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bennymartensson/32/1317_2.png) [@bennymartensson](https://forum.kirupa.com/u/bennymartensson)\
**Post date:** [June 12, 2008, 10:00pm UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/1 "2008-06-12T22:00:54Z")

</div>

Possible to add my mc’s in sequence?

```auto

for (var i:Number = 1; i < 10; i++) {

var b:Bubbla = new Bubbla();
b.name = "tanke"+i;
addChild(b);
}

```

---

<div class="post-metadata">

**Author:** ![Anogar](https://avatars.discourse-cdn.com/v4/letter/a/ed655f/32.png) [@Anogar](https://forum.kirupa.com/u/Anogar)\
**Post date:** [June 12, 2008, 10:07pm UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/2 "2008-06-12T22:07:09Z")

</div>

As opposed to naming them I like to push them into an array, like this:

```auto

var bubblaArray:Array = [];

for (var i:Number = 1; i < 10; i++) 
{
     var b:Bubbla = new Bubbla();
     addChild(b);
     bubblaArray.push(b);
}

```

Anyhow, you’ve got the right idea though.

---

<div class="post-metadata">

**Author:** ![bennymartensson](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bennymartensson/32/1317_2.png) [@bennymartensson](https://forum.kirupa.com/u/bennymartensson)\
**Post date:** [June 13, 2008, 8:04am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/3 "2008-06-13T08:04:18Z")

</div>

Brilliant, thanks! But how do I name each mc and place them at their unique x and y?

[quote=Anogar;2341793]As opposed to naming them I like to push them into an array, like this:

```
ActionScript Code:
[LEFT][COLOR=#000000] **var** [/COLOR] bubblaArray:[COLOR=#0000FF]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR];

```

[COLOR=#0000FF]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000FF]Number[/COLOR] = [COLOR=#000080]1[/COLOR]; i \< [COLOR=#000080]10[/COLOR]; i++[COLOR=#000000])[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] b:Bubbla = [COLOR=#000000] **new** [/COLOR] BubblaCOLOR=#000000[/COLOR];  
addChildCOLOR=#000000[/COLOR];  
bubblaArray.[COLOR=#0000FF]push[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

Anyhow, you’ve got the right idea though.[/quote]

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 13, 2008, 8:09am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/4 "2008-06-13T08:09:27Z")

</div>

you could do what you did before:  
[AS]  
var bubblaArray:Array = [];

for (var i:Number = 1; i \< 10; i++)  
{  
var b:Bubbla = new Bubbla();  
b.name = “tanke” & i;  
b.x = 34;  
b.y = 89;  
addChild(b);  
bubblaArray.push(b);  
}  
[/AS]

but I don’t work that much with names anymore, I rely more on the objects themselves and pass a reference to an object then the name of it.

hth  
Carlo

---

<div class="post-metadata">

**Author:** ![pensamente](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/pensamente/32/1208_2.png) [@pensamente](https://forum.kirupa.com/u/pensamente)\
**Post date:** [June 13, 2008, 8:11am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/5 "2008-06-13T08:11:33Z")

</div>

[edit]  
same answer above…

:]

🙂

---

<div class="post-metadata">

**Author:** ![bennymartensson](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bennymartensson/32/1317_2.png) [@bennymartensson](https://forum.kirupa.com/u/bennymartensson)\
**Post date:** [June 16, 2008, 7:05am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/6 "2008-06-16T07:05:09Z")

</div>

Ok, thanks. So far so good, but lets say I want to place 10 mc’s and want everyone to be placed at their unique x and y, how to do this? This one places all at the same spot…

Do I create another Array with the position or something?

thx

[quote=hasch2o;2342021]you could do what you did before:  
ActionScript Code:  
[LEFT][COLOR=#000000] **var** [/COLOR] bubblaArray:[COLOR=#0000ff]Array[/COLOR] = [COLOR=#000000][[/COLOR][COLOR=#000000]][/COLOR];

[COLOR=#0000ff]for[/COLOR] [COLOR=#000000]([/COLOR][COLOR=#000000] **var** [/COLOR] i:[COLOR=#0000ff]Number[/COLOR] = [COLOR=#000080]1[/COLOR]; i \< [COLOR=#000080]10[/COLOR]; i++[COLOR=#000000])[/COLOR]  
[COLOR=#000000]{[/COLOR]  
[COLOR=#000000] **var** [/COLOR] b:Bubbla = [COLOR=#000000] **new** [/COLOR] BubblaCOLOR=#000000[/COLOR];  
b.[COLOR=#0000ff]name[/COLOR] = [COLOR=#ff0000]“tanke”[/COLOR] & i;  
b.[COLOR=#000080]x[/COLOR] = [COLOR=#000080]34[/COLOR];  
b.[COLOR=#000080]y[/COLOR] = [COLOR=#000080]89[/COLOR];  
addChildCOLOR=#000000[/COLOR];  
bubblaArray.[COLOR=#0000ff]push[/COLOR]COLOR=#000000[/COLOR];  
[COLOR=#000000]}[/COLOR]  
[/LEFT]

but I don’t work that much with names anymore, I rely more on the objects themselves and pass a reference to an object then the name of it.

hth  
Carlo[/quote]

---

<div class="post-metadata">

**Author:** ![ironchefmoto](https://avatars.discourse-cdn.com/v4/letter/i/91b2a8/32.png) [@ironchefmoto](https://forum.kirupa.com/u/ironchefmoto)\
**Post date:** [June 16, 2008, 7:28am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/7 "2008-06-16T07:28:22Z")

</div>

[QUOTE=Anogar;2341793]As opposed to naming them I like to push them into an array, like this:

```auto

var bubblaArray:Array = [];

for (var i:Number = 1; i < 10; i++) 
{
     var b:Bubbla = new Bubbla();
     addChild(b);
     bubblaArray.push(b);
}

```

[/QUOTE]

Is this better practice in general? Versus trying to give the randomly numbered instance names for dynamic MCs and Sprites .name properties? I guess it does move away from the whole concept of instance names and moves more into the programmatic reference of objects on the stage, yes?

In the case of the original poster, I think you’d be able to access the object properties in the array – things like x, y, alpha, etc. That’s typically how it works with passing objects into an array.

Thanks,  
IronChefMorimoto

---

<div class="post-metadata">

**Author:** ![hasch2o](https://avatars.discourse-cdn.com/v4/letter/h/ba8739/32.png) [@hasch2o](https://forum.kirupa.com/u/hasch2o)\
**Post date:** [June 16, 2008, 7:29am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/8 "2008-06-16T07:29:17Z")

</div>

Yes, the one that I wrote puts it on the same spot, but you can modify it so that it changes the position.

```auto
var bubblaArray:Array = [];

for (var i:Number = 1; i < 10; i++)
{
var b:Bubbla = new Bubbla();
b.name = "tanke" & i;
//change the x and y property with the code below
//b.x = 34;
//b.y = 89;
addChild(b);
bubblaArray.push(b);
}

```

It depends though how you want your sprites to be aligned:  
horizontally:

```auto

b.x = i * 20; //change 20 to the distance between the sprites
b.y = 0;

```

vertically:

```auto

b.x = 0; 
b.y = i * 20;//change 20 to the distance between the sprites

```

in rows with 3 sprites:

```auto

b.x = ((i-1) % 3) * 20;
b.y = Math.floor((i-1)/3) * 40;
//change 3 to the number of sprites per row and 20/40 to the distance between sprites

```

hth

Carlo

---

<div class="post-metadata">

**Author:** ![bennymartensson](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bennymartensson/32/1317_2.png) [@bennymartensson](https://forum.kirupa.com/u/bennymartensson)\
**Post date:** [June 16, 2008, 10:35am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/9 "2008-06-16T10:35:18Z")

</div>

Ok, thanks for that but still it doesn’t really do what i want. My thing is that I want to place each at a unique x and y, let’s say mc1 at x=54, y=76 and mc2 at x=87, y=654 etc.

Can I go into my Array somehow and modify the placement? Like:

```auto
 bubblaArray:[COLOR=#0000ff]Array[/COLOR] = [[COLOR=#000000][x=54, y=76[/COLOR][COLOR=#000000]], [x=87, y=654]];[/COLOR]

```

???

---

<div class="post-metadata">

**Author:** ![Iamthejuggler](https://avatars.discourse-cdn.com/v4/letter/i/ecc23a/32.png) [@Iamthejuggler](https://forum.kirupa.com/u/Iamthejuggler)\
**Post date:** [June 16, 2008, 10:51am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/10 "2008-06-16T10:51:13Z")

</div>

Where are you getting these unique x and y positions from exactly? Are they randomly generated, or stored in an xml maybe?

If you already know where they need to be placed then you can store those as an array like you suggested.

```auto

var bubblaArray:Array = [];
// the following array has all the x,y coords you want to use.
var bubblaPositions:Array = [[3,4],[46,24],[160,86] .... etc etc];

for (var i:Number = 1; i < 10; i++) 
{
     var b:Bubbla = new Bubbla();
     b.x=bubblaPositions*[0];
     b.y=bubblaPositions*[1];
     addChild(b);
     bubblaArray.push(b);
}

```

editted out sloppy coding mistakes!

---

<div class="post-metadata">

**Author:** ![bennymartensson](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/bennymartensson/32/1317_2.png) [@bennymartensson](https://forum.kirupa.com/u/bennymartensson)\
**Post date:** [June 17, 2008, 9:26am UTC](https://forum.kirupa.com/t/addchild-in-sequence/263147/11 "2008-06-17T09:26:03Z")

</div>

Ok, thanks you all! Just want to share my final code in this matter. I wanted to place my mc’s at their own unique x and y and put them on the stage at different times. This was my solution, please feel free to tell me if this can be made in a smarter way… 🙂

```auto

var bubblaArray:Array = [];
var bubblaData:Array = [[56,432,.1],
                            [182,408,.2],
                            [322,245,.3],
                            [334,427,.4],
                            [522,636,.5],
                            [605,489,.6],
                            [735,185,.7],
                            [880,159,.8],
                            [935,422,.9]];

for (var i:Number = 0; i < 9; i++) 
{
     var b:Bubbla = new Bubbla();
     var placeBubbla = { scaleX:1, scaleY:1, time:2, transition:"easeoutelastic", delay:bubblaData*[2] }
     b.scaleX = b.scaleY = 0;
     b.x = bubblaData*[0];
     b.y = bubblaData*[1];
     Tweener.addTween( b, placeBubbla );
     addChild(b);
     bubblaArray.push(b);
}

```
