0

I have a bunch of movie clips on a layer named s1, s2, s3, .. s16.

I need to add them to an array so that I can select a random number to play.

Something like:

var sparkle:Array   =   new Array();
var i:int = new int;
for(i=0;i=16;i++)
{
    sparkle[i] = 's'+i;
}

2 Answers 2

2

If you know the total number of MovieClips, you don't need to store them in an Array.

Get your random number first

var random:int = Math.round( Math.random() * totalNumber );

Then you can use a function to return the MC you want

private function getMovieClip(value:int ):MovieClip
{
    var mc:MovieClip;

    for( var i:int ; i < totalNumber ; ++i )
    {
         if( this.getChildAt(i).name == "s"+ random.toString() )
         {
             mc = this.getChildAt( i ) as MovieClip;
             break;
         }
    }

    return mc;
}
1

Try this:

var sparkle:Array = [];

var n:int = 16;//numberOfClips;

for (var i:int = 0; i < n; i++) {

var tempClip:MovieClip = MovieClip(getChildByName("s"+i) );

sparkle[i] = tempClip;

}

You can then loop through your array to reference the clips

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.