juniorspecial
juniorspecial

Reputation: 11

how to call objects created in a loop?

Newbie question:

If I create several shape objects in a loop, like:

var i:int; 
for (i = 0; i < 3; i++) {
    var circle:Shape = new Shape();
    circle.graphics.beginFill(color);
    circle.graphics.drawCircle(100,100, radius);
    circle.graphics.endFill();
    addChild(circle);   
}

How can I then call those different shapes separately, so I could manipulate their properties? It would seem to me they would all have the same name?

Upvotes: 1

Views: 148

Answers (3)

Abdullah Elameer
Abdullah Elameer

Reputation: 1

for(int i=0;i<3;i++)
{
class x =new class(i);
x.print()
}

Upvotes: -1

KumoKairo
KumoKairo

Reputation: 678

Actually, you can just have an array (or vector) of objects, so you won't depend on current displaylist.

var i:int; 
var circleArray:Array = new Array();
for (i = 0; i < 3; i++) {
    var circle:Shape = new Shape();
    circleArrayList.push(circle);
    circle.graphics.beginFill(color);
    circle.graphics.drawCircle(100,100, radius);
    circle.graphics.endFill();
    addChild(circle);   
}
//And then access them with
for(i = 0; i < circleArray.length, i++)
{
    circleArrayList[i].x = 15 * i;
}
//Or
foreach(var circle:DisplayObject in circleArray)
{
    circle.x = 15 * i;
}

Upvotes: 1

putvande
putvande

Reputation: 15213

You can access them via their index (the order they have been put on the stage). So something like:

DisplayObject(getChildAt(1)).x = 100; // Where the 1 is the index (starting at 0)

Upvotes: 1

Related Questions