• 10
• 9
• 13
• 10
• 18

[XNA] Input Issue

This topic is 2763 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I have a menu that is displayed on screen and for some reason when I press down instead of highlighting the next option in Orange, it goes haywire and starts flickering like it's trying to switch options at lightning fast speed.

My update method has the following
                 if (keyboard.IsKeyDown(Keys.Down))                    {                        remDownPressed = true;                        remUpPressed = false;                    }                    if (keyboard.IsKeyDown(Keys.Up))                    {                        remDownPressed = false;                        remUpPressed = true;                    }

My draw method has the following

       if (remDownPressed == true)                    {                        if (currentMenuItem == 0)                        {                            currentMenuItem = 1;                        }                        else if (currentMenuItem == 1)                        {                            currentMenuItem = 2;                        }                        else if (currentMenuItem == 2)                        {                            currentMenuItem = 0;                        }                    }                    if (remUpPressed == true)                    {                        if (currentMenuItem == 0)                        {                            currentMenuItem = 2;                        }                        else if (currentMenuItem == 1)                        {                            currentMenuItem = 0;                        }                        else if (currentMenuItem == 2)                        {                            currentMenuItem = 1;                        }                    }                      switch (currentMenuItem)                    {                         case 0:                           //Draw Menu with option 0 highlighted in Orange                            break;                        case 1:                        //Draw Menu with option 1 highlighted in Orange                            break;                        case 2:                          //Draw Menu with option 2 highlighted in Orange                            break;                        default:                           //Draw Menu with option 0 highlighted in Orange                             break;                    }

Share on other sites
Your code do what you told it to do. Dont forget that the system is calling the Update-Draw pair as fast as it can. Hence, the lighting fast menu switch you see.

It's alot simpler and elegant to simply do it like..
// Holds KeyboardState from last frame.KeyboardState _keyboardOldState;override void Update(GameTime gameTime){   // gets input for this frame  KeyboardState keyboardState = Keyboard.GetState();  // process inputs  if( keyboardState.IsKeyDown(Keys.Up)       && keyboardOldState.IsKeyUp(Keys.Up))  {     currentMenuItem ++;  }  if( keyboardState.IsKeyDown(Keys.Down)      && keyboardOldState.IsKeyUp(Keys.Down))  {    currentMenuItem --;   }  // keep this frame inputs as next frame old input  _keyboardOldState = keyboardState;}

Normally people will wrap up this OldKey "pattern" into some class input manager or whatnot.

Also try not to mix your code in Update and Draw. Draw shouldnt have "logic" code (the menu selection code).

Hope it helps,