I have an array with four different MovieClips in:
var myEnemy_1:Enemy_1;
var myEnemy_2:Enemy_2;
var myEnemy_3:Enemy_3;
var myEnemy_4:Enemy_4;
var enemyArray:Array = [new Enemy_1(stage), new Enemy_2(stage), new Enemy_3(stage), new Enemy_4(stage)];
Now i want to pick on of the movieclips each second and place it on my stage.
var myTimer:Timer = new Timer(1000, 1000);
var randomNumber:int;
myTimer.addEventListener("timer", timedFunction);
function timedFunction(myTime:Event):void {
randomNumber = Math.random()*4;
stage.addChild(enemyArray[randomNumber]);
enemyArray[randomNumber].x = stage.stageWidth;
enemyArray[randomNumber].y = Math.random()*(stage.stageHeight - 50) + 10;
enemyArray.push(enemyArray[randomNumber]);
}
myTimer.start();
In each of the MovieClips own class i have them movieng at a speed;
x -= Math.random()*4+1;
Everything works great except that when one of the movieclips is on the stage and the randomNumber get the same movieclip, from the array, the movieclip is removed from stage and added again (at its start position).
What i want it to do, is to let the movieclip be on the stage and just add another copy of the it, so there is two identical movieclips on the stage.
Hope someone has an answer for this, or a work around =) Thanks a lot!
If you truly want a new copy of the same Class, you need to create a new instance. So:
var enemyArray:Array = [Enemy_1, Enemy_2, Enemy_3, Enemy_4];
function timedFunction(myTime:Event):void {
randomNumber = Math.random()*4;
var enemy:AbstractEnemy = new enemyArray[randomNumber](stage);
stage.addChild(enemy);
enemy.x = stage.stageWidth;
enemy.y = Math.random()*(stage.stageHeight - 50) + 10;
someOtherArray.push(enemy); //don't want to add this to your array of Class definitions
}
However, you might also want to google blitting and object pooling for other, more efficient techniques.