Quantcast
Channel: Community | MonoGame - Latest topics
Viewing all articles
Browse latest Browse all 6821

[SOLVED] I want to repeat a SoundEffect a specified # of times

$
0
0

@archnem wrote:

Hi all:

I have been knocking the rust off my XNA/Monogame skills recently, and have been working on a simple game where I want a bell to sound based on how the player does. For instance, if they strike 3 targets, I want the bell to ring 3 times, or 5 times if they strike 5 targets etc. I hacked together a bit of code that works:

public static void RepeatNamedSound(string name, int times)
    {
        int counter = 0;
        if (namedEffects.Count > 0)
        {
            if(namedEffects[name] != null)
            {
                while(counter<times)
                {
                    if(namedEffects[name].State != SoundState.Playing)
                    {
                        namedEffects[name].Play();
                        counter++;
                    }
                }
            }
        }
    }

But this of course has the effect of "locking" my game until the sound finishes playing the specified # of times. So when I call this function, I just ended up creating a thread to do it like so:

Thread thread = new Thread(() => AudioManager.RepeatNamedSound("ShortBell", counter));
thread.Start();

This seems a bit over-complicated to me for such a simple request, but the only other way I could think of to do it was to make an Update() function on my audio manager to run every frame and check a queue of requested sounds for what has played/remains etc. I had wanted to make my audio manager static to function in a kind of fire-and-forget way, so I ended up taking the other approach.

Has anyone else done this? Is there something simple I'm missing here, such as a way to specify the # of times IsLooped = true will loop a sound? I know in XACT this was possible back in the day.

Thanks in advance for reading and your help!

Posts: 5

Participants: 4

Read full topic


Viewing all articles
Browse latest Browse all 6821

Trending Articles