Game Development Reference
In-Depth Information
Finally, we send a message to the ScoreBoard telling it the new value of shots , so the value can
be displayed correctly. We also dispatch a CustomSoundEvent to play the Main.SOUND_SHOOT to
play the shooting sound for the cannon.
dispatchEvent(new CustomEventScoreBoardUpdate(CustomEventScoreBoardUpdate.UPDATE_TEXT,
Main.SCORE_BOARD_SHOTS,String(shots)));
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,Main.SOUND_SHOOT,
false,1,0,.5));
Alternatively, if we do not drop into the then portion of the if:then statement (because there are
no shots left), we drop into the else and play the annoying “no shots” sound, so the player knows
the arsenal has been prematurely depleted.
} else {
dispatchEvent( new CustomEventSound(CustomEventSound.PLAY_SOUND,
Main.SOUND_NOSHOTS,false,1,0,.75));
}
}
Placing the ships
One of the final things newLevel() must do is place the player's fleet of ships on the screen. We
remove and replace the player's ships at the start of each level, so we can add ships to the fleet if
they have been earned by reaching scoreNeededForExtraShip and reposition the ships, if
necessary, according to how many the player has left.
No matter how many ships the player currently has in the fleet, only three are shown on the
screen at any one time, as defined by the maxVisibleShips setting (note that you could make this
value larger, but the ships would not all fit onto the screen). If there are less than three ships, the
placement is adjusted so that they are centered on the screen.
To handle the ship placement, we must first call placeShips() in the newLevel() function.
placeShips();
Now, we need to define the placeShips() function itself. Recall that the idea behind this function
is that we always want to place the remaining ships centered on the screen. To do this, we need
to know how many ships are left and how big the screen is that we will be placing them onto. The
good news is that we already know this information; we just need to use it properly.
The first thing we need to do is to define the function and then find out how many ships the player
has left.
private function placeShips():void {
var ctr:int;
var xSpacing:int;
var tShips:int = ships;
if (tShips > maxVisibleShips) {
tShips = maxVisibleShips;
}
We don't care if the player has more than three ships at the start of a level; we are only going to
use a maximum of three (as set in maxVisibleShips ). The rest are in reserve and will be used on
the next level. Now, we are going to start placing the ships.
xSpacing = (gameWidth/tShips);
Search WWH ::




Custom Search