HTML and CSS Reference
In-Depth Information
iif (( tSound . element . ended || ! tSound . played ) &&
tSound . name == sound ) {
soundFound = true
true ;
tSound . played = true
true ;
} else
else {
soundIndex ++ ;
}
}
}
Using this method, we play a sound only if it has not been played, it has ended, and it already
has the sound file loaded that we need to play. There is no pause to load (most of the time),
and sounds play at pretty much the time that we need them to play. If we need more sounds,
we can load more up front, or we can set MAX_SOUNDS to a number greater than the number
of preloaded sounds. If we do that, we will create new sound objects on the fly (although this
might still give you a pause when loading from a web server):
iif ( soundFound ) {
tempSound = soundPool [ soundIndex ]. element ;
tempSound . volume = volume ;
tempSound . play ();
} else
else iif ( soundPool . length < MAX_SOUNDS ){
tempSound = document . createElement ( "audio" );
tempSound . setAttribute ( "src" , sound + "." + audioType );
tempSound . volume = volume ;
tempSound . play ();
soundPool . push ({ name : sound , element : tempSound , type : audioType ,
played : true
true });
}
Go ahead and try this code. It is CH7EX9.html in the code distribution, or you can type in the
program listing.
Other stuff you could do to improve the game
The next couple chapters introduce game concepts, so we really shouldn't go much further
with Space Raiders . Still, if you were going to finish this game, you might consider doing the
following:
1. Add a score.
2. Increase the aliens' speed on each new level.
3. Collision-detect the aliens and the player.
Search WWH ::




Custom Search