-1
\$\begingroup\$

One of my game states is being skipped (pressing space goes straight to the 'GameScreen' game state skipping the 'CharSelect' state). All help is appreciated.

        if (currentGameState == GameState.TitleScreen)
        {
            if (keyboard.IsKeyDown(Keys.Space))
            {
                currentGameState = GameState.CharacterSelect;
                testFontTitle = "CharSelect";
                changedstate = true;
            }

            if (keyboard.IsKeyDown(Keys.Escape))
            {
                this.Exit();
            }
        }

        else if (currentGameState == GameState.CharacterSelect)
        {
            if (keyboard.IsKeyDown(Keys.Space))
            {
                currentGameState = GameState.GameScreen;
                testFontTitle = "GameScreen";
                changedstate = true;
            }

            if (keyboard.IsKeyDown(Keys.Escape))
            {
                currentGameState = GameState.TitleScreen;
                testFontTitle = "Title";
                changedstate = true;
            }
        }

        // TODO: Add your update logic here
        base.Update(gameTime);
    }
\$\endgroup\$
2
  • \$\begingroup\$ Are you initializing currentGameState to "GameState.TitleScreen"? \$\endgroup\$ Feb 14, 2016 at 18:44
  • \$\begingroup\$ What's the context in which this code is used? \$\endgroup\$
    – Vaillancourt
    Feb 14, 2016 at 19:01

1 Answer 1

4
\$\begingroup\$

It is probably due to your gameloop is faster then your pressing speed. You should stop detecting space key when you pressed it once. For checking purpose you can use a flag for now. Like,

bool _isKeyPressed = false;
if (currentGameState == GameState.TitleScreen && !_isKeyPressed)
    {
        if (keyboard.IsKeyDown(Keys.Space))
        {
            currentGameState = GameState.CharacterSelect;
            testFontTitle = "CharSelect";
            changedstate = true;
            _isKeyPressed = true;
        }

        if (keyboard.IsKeyDown(Keys.Escape))
        {
            this.Exit();
        }
    }

    else if (currentGameState == GameState.CharacterSelect && !_isKeyPressed)
    {
        if (keyboard.IsKeyDown(Keys.Space))
        {
            currentGameState = GameState.GameScreen;
            testFontTitle = "GameScreen";
            changedstate = true;
            _isKeyPressed = true;
        }

        if (keyboard.IsKeyDown(Keys.Escape))
        {
            currentGameState = GameState.TitleScreen;
            testFontTitle = "Title";
            changedstate = true;
        }
    }

    if (keyboard.IsKeyUp(Keys.Space))
        _isKeyPressed = false;

    // TODO: Add your update logic here
    base.Update(gameTime);
}

If you have to check more than just the spacebar to check, you may replace the flag with a 'old' and 'new' keyboard state mechanism like so:

    Keyboardstate _oldKBstate;

    protected override void Initialize()
    {
        _oldKBstate = Keyboard.GetState(); // Make sure it is initialized!

        // ...
    }

    protected override void Update(GameTime gameTime)
    {
         Keyboardstate newKBstate = Keyboard.GetState();

         // ....

         if(_oldKBstate.IsKeyDown(Keys.A) && newKBstate.IsKeyUp(Keys.A))
         {
               // On A key press.
         }

         if(_oldKBstate.IsKeyUp(Keys.A) && newKBstate.IsKeyDown(Keys.A))
         {
               // On A key release.
         }

         _oldKBstate = newKBstate; // remember the new state.
    }
\$\endgroup\$
5
  • \$\begingroup\$ Hamza's answer is the reason. To expand it: Instead of working with a flag- you can work with old and new keyboard states. This enables you to check for keypresses or keyreleases. Keyboardstate _oldKB; at the start of your update loop Keyboardstate newKB = Keyboard.GetState(); at the end of the loop _oldKB=newKB; Now you can do if(_oldKB.IsKeyUp(Keys.A)&&newKB.IsKeyDown(Keys.A)) to check a keypress and if(_oldKB.IsKeyDown(Keys.A)&&newKB.IsKeyUp(Keys.A)) to check a key release. Using this mechanism you won't have to create flags for each key you want to monitor. \$\endgroup\$
    – Felsir
    Feb 15, 2016 at 7:37
  • \$\begingroup\$ You are right bro.. But it has been a long time ago when I was developing XNA games.. So sorry for that :) \$\endgroup\$ Feb 15, 2016 at 7:39
  • \$\begingroup\$ No problem :-) I felt it was better to comment than to create a new answer since your reasoning (and code) is correct. \$\endgroup\$
    – Felsir
    Feb 15, 2016 at 7:42
  • \$\begingroup\$ Thank you bro.. I'd appreciate if you append your answer, may be everybody won't scroll down to the comments \$\endgroup\$ Feb 15, 2016 at 7:44
  • \$\begingroup\$ Added it. Awaiting peer review. \$\endgroup\$
    – Felsir
    Feb 15, 2016 at 7:55

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .