0
\$\begingroup\$

For my HND I've got to re-create Frogger, and everything was going swell until I tried adding some collision detection and sound. Whenever my player intersects a vehicle, the sound file just starts playing with every game update instead of just once.

Here is my code from the update method:

            if (vehicles[count].bbox.Intersects(playerSprites[lives].bbox))
                playerSprites[lives].isDead = true;
        }

        if (playerSprites[lives].isDead)
        {
            deathSound.Play();
        }

The sound is declared as a SoundEffect, loaded as a SoundEffect, and the content processor is set to Sound Effect, and as far as I can tell, Sound Effects in XNA are only meant to be played once. What am I doing wrong??

\$\endgroup\$
1
\$\begingroup\$

Notice that you are launching a sound effect while the player is dead, not when the player died. The obvious solution would be to call your soundEffect.Play here:

if (vehicles[count].bbox.Intersects(playerSprites[lives].bbox)){
  playerSprites[lives].isDead = true;
  deathSound.Play();
  //and also disable further intersection tests with player bbox to launch this function
  //only once. You could modify it so that its area becomes zero
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.