Game Development Reference
In-Depth Information
However, that is not all. Even if all the Enemy planes are gone, ending the level abruptly can be
jarring for the player. If there are Explosion objects or Flak still animating, Shot objects flying or a
BonusPlane flying, we want to wait until those animations end before the level is over. This also
gives the player a chance to shoot the BonusPlane one last time if it has arrived just when the
level is over. We do this by simply checking the length properties of all the associated arrays for
the objects. Also, as a quick bonus, we add 10 times the number of player's shots remaining to
the score (this is another possible difficulty setting). Finally, we dispatch a Game.NEW_LEVEL event
so that Main can take control and call newLevel() .
private function checkforEndLevel():void {
if (enemyArray.length <= 0 && incomingCount >= numEnemies
&& explodeArray.length <=0 && flakArray.length <=0 &&
shotArray.length <=0 && bonusPlaneArray.length <= 0) {
addToScore(10*shots);
cleanUpLevel();
dispatchEvent(new Event(Game.NEW_LEVEL));
}
}
You will notice in the preceding function that another function is called named cleanUpLevel() . This
function removes all the objects from the screen and removes the EventListener we created to
respond to the player shooting. We have separated this into a second function so it could be called
from the checkForEndGame() function as well as from checkForEndOfLevel() . All of the code in this
function should look very familiar to you. It is very similar to render() , in that we will loop through all
of our arrays of objects. However, since we are removing the objects from the arrays, we can't use
for:each . If we did use for:each (or a forward for:next loop) the loop will get out of sync as we
remove items resulting in the array not being fully cleaned up. Instead, we must iterate backward
through each array. For every item, we use our old friend removeItemFromArray() . Also, to be
complete, at the end of the function, we remove the listener for the MouseEvent.MOUSE_DOWN .
private function cleanUpLevel():void {
var ctr:int = 0;
for (ctr = shotArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(shotArray[ctr],shotArray);
}
for (ctr = flakArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(flakArray[ctr],flakArray);
}
for (ctr = enemyArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(enemyArray[ctr],enemyArray);
}
for (ctr = explodeArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(explodeArray[ctr],explodeArray);
}
for (ctr = shipArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(shipArray[ctr],shipArray);
}
for (ctr = bonusPlaneArray.length-1;ctr >=0;ctr--) {
removeItemFromArray(bonusPlaneArray[ctr],bonusPlaneArray);
}
stage.removeEventListener(MouseEvent.MOUSE_DOWN, shootListener);
}
Search WWH ::




Custom Search