Jump to content
  • Advertisement
Sign in to follow this  
murdock

[XNA] Input Issue

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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,

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!