I want to be able to spawn multiple enemies for a scrolling flier game I'm working on for college (about 4-5 enemies), and I figured the easier way to handle their creation is through an array. Currently all of my objects (except for myScore) are imported as MovieClip files. Right now this is what I have for enemy-associated code:
Code:
var fEnemy:FEnemy = new FEnemy();
addChild(fEnemy);
fEnemy.x = 0;
fEnemy.y = Math.random() * 150 + 80;
//---Saucer Animation---//
fEnemyMoves();
function fEnemyMoves():void
{
//fEnemySpeed = (Math.ceil(Math.random() * 6 + 1));//
fEnemy.x = (Math.ceil(Math.random() * 6)) + 640;//
fEnemy.y = (Math.ceil(Math.random() * 330)) + 50;//
Tweener.addTween(fEnemy,
{x:-50,
time:fEnemySpeed,
transition:"linear",
onComplete:fEnemyMovesLoops});
}
function fEnemyMovesLoops():void
{
//fEnemySpeed = (Math.ceil(Math.random() * 6 + 1));//
fEnemy.x = (Math.ceil(Math.random() * 6)) + 640;//
fEnemy.y = (Math.ceil(Math.random() * 330)) + 50;//
Tweener.addTween(fEnemy,
{x:-50,
time:fEnemySpeed,
transition:"linear",
onComplete:fEnemyMoves});
}
//---End Saucer Animation---//
fireBall.addEventListener(Event.ENTER_FRAME, killEnemy);
function killEnemy(evt:Event): void
{
if(fireBall.hitTestObject(fEnemy))
{
fireBall.removeEventListener(Event.ENTER_FRAME, fireFBall);
fireFlag = 0;
fEnemyMoves();
scores++;
if (scores == 3)
{
stop();
gotoAndPlay(6);
}
}
myScore.score_txt.text = "score:" + String(scores);
}
Now, if I change the original declaration to:
Code:
for(var i:int = 0; i<3; i++)
{
var fEnemy:FEnemy = new FEnemy();
addChild(fEnemy);
fEnemy.x = 0;
fEnemy.y = Math.random() * 150 + 80;
}
only one of the enemies will spawn normally. The other two appear, but they only hang around at the other end of the screen. I dont recall the code, but I had been able to (at one point) get all three enemies to start from the right end and go all the way over to the left, but then the game continued with spawning only one enemy again. We haven't learned much about arrays in our classes unfortunately, so I was wondering if I could get advice here.