icekomo
icekomo

Reputation: 9466

Push instance name of movieClip into array using a for-loop

I have a bunch of movieclips on the stage with instance names ball1 - ball200. I was hoping I didn't have to create an array and manually set all the instance names into the array

ballArray = [ball1, ball2,ball3, etc];

I was trying to get a for loop to cycle through and add each instance name to my array like so:

function createTheArray():void{
    for(var i:int = 1; i < 20;i++){
        ballArray.push(ball + i);
        trace(newArray[i])
    }
}

But I keep getting back undefined array index's. It also tells me that I doesn't know what "ball" is. How would you use part of a instance name and combine it with the index value of the loop. So that the first time through you get ball1 as the first index value of your array?

Upvotes: 1

Views: 1411

Answers (2)

BadFeelingAboutThis
BadFeelingAboutThis

Reputation: 14406

Dragging out 200 balls onto the timeline and giving them instance names doesn't sound like much fun!

BEST OPTION:

right click the ball object and go to the properties, click "export for actionscript" and give it a unique name. (Lets call it MyBall for this example)

in your timeline code do this:

   var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
   for(var i:int=0;i<200;i++){
       ballArray.push(new MyBall());
       addChild(ballArray(ballArray.length-1));
   }

NEXT BEST OPTION

if all your balls are on the timeline already, you can still do the step from above (export for actionScript and give it a name) but do the following code:

var ballArray:Vector.<MyBall> = new Vector.<MyBall>();
var i:int = numChildren;
while(i--){
    if(this.getChildAt(i) is MyBall) ballArray.push(this.getChildAt(i) as MyBall);
}

ANOTHER OPTION

If your balls are not all the same library objects, if you put them all as the only objects in a movie clip container (let's say you gave it the instance name ballContainer, you can still use this code so you don't have to give them instance names:

var ballArray:Vector.<DisplayObject> = new Vector.<DisplayObject>();
var i:int = ballContainer.numChildren;
while(i--){
    ballArray.push(ballContainer.getChildAt(i));
}

Upvotes: 2

Karmacon
Karmacon

Reputation: 3190

You can use a string in brackets to get a property of an object. In your case, your object is referred to as this. So your syntax for getting a ball is this["ball"+index]. Try this:

function createTheArray():void{

    for(var i:int = 1; i < 20; i++){
        ballArray.push(this["ball" + i]);
    }
    trace(ballArray);
}

Referencing Properties by String isn't really a great practice though. If it's possible to create your balls dynamically as well, that would be a better implementation. You can create a ball MovieClip on your timeline, and select Export For ActionScript in the properties. Then you can use this code to instantiate 20 or more balls:

//add 20 balls to stage
var ballArray:Array = [];
for(var i:int = 0; i < 20; i++){
    var ball:Ball = new Ball();
    addChild(ball);
    ballArray.push(ball);
}
trace(ballArray);

Upvotes: 1

Related Questions