Playing the rest of the sounds
Now, we will add the rest of the calls to the play
function. We will address each of them individually, as pinpointing exactly where they go in the code is crucial to using them at the right moment.
Adding sound effects while the player is reloading
Add the following highlighted code in three specific locations to trigger the appropriate reload or reloadFailed
sound when the player presses the R key to attempt reloading their gun:
Tif (state == State::PLAYING)
{
// Reloading
if (event.key.code == Keyboard::R)
{
if (bulletsSpare >= clipSize)
{
// Plenty of bullets. Reload.
bulletsInClip = clipSize;
bulletsSpare -= clipSize;
reload.play();
}
else if (bulletsSpare > 0)
{
// Only few bullets left
bulletsInClip = bulletsSpare;
bulletsSpare = 0;
reload.play();
}
...