1

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?

0

2 Answers 2

2

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));
}
Sign up to request clarification or add additional context in comments.

1 Comment

If you're not aware, Vector is basically the same as Array except all the items in the array have to be the specified kind of item
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.