soundEffect from update method keeps looping

93 Views Asked by At

I'am building a game in Xna, and my soundeffects keep looping, it is quite obvious because, the sounds are playing from an update method.

The sounds are not really playing 1 at a time, but like when my car collides to a tile for example named: tile 1, it will play like a 1000+ times at once.

How can I prevent the sound from playing 1000+ times at once, and just make it play 1x at a time?

this is the code I use to play the sound effect:

if (map[x][y] == 1)
        {
            car.speed = 0;
            crash.Play();
        }

crash is the name of the soundEffect.

Thanks in Advance!

2

There are 2 best solutions below

2
jgallant On

Quick example of how you could achieve this:

bool soundPlayed = false;   //This would be in a INIT method somewhere

if (map[x][y] == 1)
{
    if (!soundPlayed)
    {
        car.speed = 0;
        crash.Play();
        soundPlayed = true;
    }
}
else
{
    soundPlayed = false;    //Reset once condition is false
}
0
pix On

I would have to recommand to you to read a good tutorial about Xact

You'll think this method is harder than the one you are trying to use, but it's going to be more flexible for you in the futur :)

Xact on MSDN

Or you can use the pause() method from your sound.