5

I have just run through the very useful begginers tutorial at http://www.riemers.net/eng/Tutorials/XNA/Csharp/series2d.php and am reasonably happy with the outcome.

However, I have noticed that by using 3 .wav files for the sound effects, the game is massively slowed down, I have adapted this bit of code:

if (keybState.IsKeyDown(Keys.Enter) || keybState.IsKeyDown(Keys.Space))
        {
            if (!rocketFlying) launch.Play();
            rocketFlying = true;    
            ...
         }

which plays the launch soundeffect when the button is pressed, as the update method check this 60 times a second, and it is hard to press the button that quickly, so it now only plays it once.

However, it still slows the game down massively, which is obviously not ideal.

Obviously, I am not trying to do anything with this particular game, but I would like to know how to sort out this problem for future projects and just because it is a tad irritating.

Cheers Ryan

Ryan Durrant
  • 878
  • 4
  • 12
  • 23
  • 1
    I don't know if this will fix your problem, but looking at that code, you need to check if the last KeyboardState shows those buttons to be up, or you could implement a delay between when you could press the button. – annonymously Dec 23 '11 at 00:06
  • Turn off key repeating in your system and try it again. If that fixes it, then somewhere your app is trying to play the sound way more often than intended, even if you're only hearing it once. – jefflunt Dec 23 '11 at 16:14

1 Answers1

0

here is the problem: say you are running at 60 fps. if you hold down the key for 1 second, your sound effect will fire 60 times (note you are only checking .IsKeyDown, to only check once you need to check if it was just pressed, not if it's down)

so you have a couple solutions:

1) you only emit on the first press

2) you go into your xact project and limit the maximum instances of your cue, or the category, to something reasonable (3 is a good number, using replace oldest)

while you may be tempted to do #1, the best solution is #2, as it introduces you to how to properly limit sound emissions globally, plus there is a lot of cool related xact stuff you can do when you know (vary pitch, randomly pick other sounds, etc).

JasonS
  • 7,443
  • 5
  • 41
  • 61
  • Here is an article that shows exactly how to do this: http://xna-uk.net/blogs/offbyone/archive/2010/03/26/sound-in-xna-3-1-part-ii.aspx plus, it contains explanations of the other nifty xact features i mentioned. – JasonS Dec 26 '11 at 07:59
  • Just personally I would probably do both. Because of equal importance is to learn how to trap specific events happening in a game. But great reply, +1 –  Dec 28 '11 at 11:21