S At
S At

Reputation: 467

How To Create An Array of Movie Clip with number

I have multi movieclips in my stage and they have instanceName e.g. k1,k2,... . I want to createar=[k1,k2,k3,k4, ...].

var i: int;
var ar: Array = new Array();

for (i = 1; i < 5; i++)
{
    ar[i-1] = ["k" + i];
}
trace(ar);
ar[1].x = 100;

But the end of code does not perform.

Upvotes: 0

Views: 83

Answers (1)

Nbooo
Nbooo

Reputation: 865

What you want to do is to create an array of MovieClips, but instead of it you create an array of Arrays of Strings. To achieve your goal, you need to find an instance of a clip on stage by its name. Here is how you can try to do that:

const array:Array = new Array();
for(var i:int = 0; i < 5; i++) {
    const childName:String = "k" + (i + 1);
    const myMovieClip:MovieClip = stage.getChildByName(childName) as MovieClip;
    array.push(myMovieClip);
}

Upvotes: 2

Related Questions