HTML and CSS Reference
In-Depth Information
explodeSound3.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound2.removeEventListener("canplaythrough",itemLoaded,false);
saucershootSound3.removeEventListener("canplaythrough",itemLoaded,false);
soundPool.push({name:"explode1", element:explodeSound, played:false});
soundPool.push({name:"explode1", element:explodeSound2, played:false});
soundPool.push({name:"explode1", element:explodeSound3, played:false});
soundPool.push({name:"shoot1", element:shootSound, played:false});
soundPool.push({name:"shoot1", element:shootSound2, played:false});
soundPool.push({name:"shoot1", element:shootSound3, played:false});
soundPool.push({name:"saucershoot", element:saucershootSound,
played:false});
soundPool.push({name:"saucershoot", element:saucershootSound2,
played:false});
soundPool.push({name:"saucershoot", element:saucershootSound3,
played:false});
switchGameState(GAME_STATE_TITLE)
}
}
In this function, we first remove the event listener from each loaded item, then add the
sounds to our sound pool. Finally, we call the switchGameState() to send the game to
the title screen.
Playing sounds
Sounds will be played using the playSound() function from Chapter 7 . We will not
reprint that function here, but it will be in Example 9-1 where we give the entire set of
code for the game. We will call the playSound() function at various instances in our
code to play the needed sounds. For example, the createExplode() function presented
earlier in this chapter included this line:
playSound(SOUND_EXPLODE,.5);
When we want to play a sound instance from the pool, we call the playSound() function
and pass in the constants representing the sound and the volume for the sound. If an
instance of the sound is available in the pool, it will be used and the sound will play.
Now, let's move on to another type of application pool—the object pool.
Pooling Object Instances
We have looked at object pools as they relate to sounds, but we have not applied this
concept to our game objects. Object pooling is a technique designed to save processing
time, so it is very applicable to an arcade game application such as the one we are
building. By pooling object instances, we avoid the sometimes processor-intensive task
of creating object instances on the fly during game execution. This is especially appli-
cable to our particle explosions, as we create multiple objects on the same frame tick.
Search WWH ::




Custom Search