HTML and CSS Reference
As of this writing, the Opera browser in Windows offers the best support
for playing sounds. If you are having trouble with the sound in this game,
any other sound example in the topic, or in your own games, please test
them out in the Opera browser.
The sounds for our game
We will be adding three sounds to our game:
• A sound for when the player shoots a projectile ( shoot1.mp3 , .ogg , .wav )
• A sound for explosions ( explode1.mp3 , .ogg , .wav )
• A sound for when the saucer shoots a projectile ( saucershoot.mp3 , .ogg , .wav )
In the file download for this chapter, we have provided each of the three sounds in three
different formats: .wav , .ogg , and .mp3 .
Adding sound instances and management variables to the game
In the variable definition section of our game code, we will create variables to work
with the sound manager code from Chapter 7 . We will create three instances of each
sound that goes into our pool:
We also need to create an array to hold our pool of sounds:
var soundPool = new Array();
To control which sound we want to play, we will assign a constant string to each, and
to play the sound, we only ever need to use the constant. This way, we can change the
sound names easily, which will help in refactoring code if we want to modify the sounds
at a later time:
const SOUND_EXPLODE = "explode1";
const SOUND_SHOOT = "shoot1";
const SOUND_SAUCER_SHOOT = "saucershoot"
Finally, we need a variable called audioType , which we will use to reference the current
file type ( .ogg , .mp3 , or .wav ) by the sound manager code.