I following a guide to display seconds in XNA
, but was now wondering how to make the seconds count-up ONLY when I press Spacebar. So, when I start the game, the seconds don't start automatically, but only when I press the spacebar.
Is it possible to do this?
This will do what you ask. All I did was add a timerEnabled
variable and set to true in the updated method if the SpaceBar was pressed. I also added a line that won't start the timer until timerEnabled
is set to true.
public class Game1 : Microsoft.Xna.Framework.Game
{
//Put this variable at the top with the others.
bool timerEnabled;
protected override void Update( GameTime gameTime )
{
if ( Keyboard.GetState().KeyDown( Key.Space ) ) timerEnabled = true;
if (timerEnabled) _timer += gameTime.ElapsedGameTime.TotalMilliseconds;
base.Update( gameTime );
}
}