Ian Wood
Ian Wood

Reputation: 6573

loop through instances of movieclips flash

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

Upvotes: 0

Views: 955

Answers (2)

Martin Coulthurst
Martin Coulthurst

Reputation: 451

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

Upvotes: 1

PatrickS
PatrickS

Reputation: 9572

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

Upvotes: 2

Related Questions