addChild in sequence?

Possible to add my mc’s in sequence?


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

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

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


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.

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]

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

[edit]
same answer above…

:]

:slight_smile:

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]

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


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

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

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:


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

vertically:


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

in rows with 3 sprites:


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

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:

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

???

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.


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!

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… :slight_smile:


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);
}