title: Control sound to only play once in XNA
body: <p>I use a sound each time a key is pressed to fire a missile. But it doesn't sound nice. I guess it's because the the sound is repeated so many times while the key is pressed down and that the code is within the Update method. I'm looking for a simple solution to just play the sound once when key is pressed? (I have tested to use a boolean variable to be true the first time and then false after det the sound has been played, but this didn't worked well because, when and where should I set it to true again) Help is preciated!</p>
  
  <pre><code>// Fire
  if (keyboardState.IsKeyDown(Keys.Space))
  {
  missile.launchMissile(spaceship.spaceshipPosition, spaceship.spaceshipDirection);
  
  soundExplosion.Play();
  }
  </code></pre>
  
  <p>EDIT New code that isn't working!?</p>
  
  <pre><code>            KeyboardState keyboardState = Keyboard.GetState();
  
          KeyboardState prevKeyboardState;
  
          prevKeyboardState = keyboardState;
  
          keyboardState = Keyboard.GetState();
  
  
          if (keyboardState.IsKeyDown(Keys.Space) &amp;&amp; prevKeyboardState.IsKeyUp(Keys.Space))
          {
              missile.launchMissile(spaceship.spaceshipPosition, spaceship.spaceshipDirection);
              soundExplosion.Play();
          }
  </code></pre>