1
votes

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.

2
If you want to add your sprite multiple times you need multiple instances. - Florent

2 Answers

2
votes

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

4
votes

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