1

I'm using a fairly standard piece of code to add a movieclip to the stage using a library link. It's fairly standard code:

var Beat:beat = new beat();

The trouble is, however that it only adds once, where I need it to add multiple times. How would I go about adding many seperate instances of the 'beat' movieclip to the stage, without making more of them/ more variables.

Florent
  • 12,310
  • 10
  • 49
  • 58
Overt_Agent
  • 510
  • 6
  • 15

2 Answers2

4

create them in the loop

var _nHowMany:int = 10;
for(var i:int = 0; i < _nHowMany; i++)
{
    addChild(new beat() as DisplayObject);
}

also you can store the reference to them if you need to use it later in a list e.g. Vector.<beat> but if not needed then simply create and add to stage (or other container).

best regards

Lukasz 'Severiaan' Grela
  • 6,078
  • 7
  • 43
  • 79
2

You can't have multiple instances of a movieclip on the stage without declaring multiple instances in your code, you can use a for loop and store all the movie clips in a single array though:

var numOfClips:Number = 5;
var mcArray:Array = new Array();

for(var i=0; i<numOfClips; i++)
{
  var newMC:beat = new beat();
  addChild(newMC);
  mcArray.push(newMC);
}

Using the above code you end up with a single array to access all 5 movie clips (cleaner than having 5 completely seperate objects like beat1, beat2, beat3).

More info on arrays: http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/Array.html

Simon McArdle
  • 893
  • 6
  • 21