Once all enemies have been destroyed, the player must be able to play the next level, with more enemies moving faster.
For our convenience, we should manage level creation with a function, changing Main
function removing the for
loop and adding a new function called playLevel
.
public function Main() {
placeSpaceship();
playLevel();
addEventListener(Event.ENTER_FRAME,onEnterFrm);
stage.addEventListener(MouseEvent.CLICK,onMouseCk);
}
This function is just a cut/paste of the for
loop previously included in Main
function, but this way we can call playLevel
from elsewhere.
private function playLevel():void { for (var i:uint=1; i<level+3; i++) { placeEnemy(i); } }
And in this specific case, we are calling it from onEnterFrm
function once we removed an enemy:
private function onEnterFrm(e:Event) { ... if (enemyToRemove>=0) { enemyVector.splice(enemyToRemove,1); enemyToRemove=-1; if (enemyVector.length==0) { level++; playLevel(); ...