Sign in to follow this  

C# Switch Case Not Working Right - Help

This topic is 2321 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

Hey everyone. Im trying to make a main menu where when i press "Enter" it will switch to the game but its not working. The code i have in the update method is:

[code]
switch (mCurrentScreen)
{
case ScreenState.MainMenu:
{
if (currentKeyboardState.IsKeyDown(Keys.Enter) && previousKeyboardState.IsKeyDown(Keys.Enter))
{
mCurrentScreen = ScreenState.Game;
}
break;
}
case ScreenState.Game:
{
UpdateGame();
break;
}
}
[/code]

The drawing method works because I took out the keyboard check and it worked fine. Can someone tell me why its not working? Thanks!

Share this post


Link to post
Share on other sites
[quote name='Nypyren' timestamp='1312937043' post='4846975']
Where do you assign currentKeyboardState and previousKeyboardState?
[/quote]

This code is inside of the main update method in game1.cd. The current and previous keyboard state is declared at the top as a public variable. Also the current and previous keyboard state are used later on in the update function and work fine

Share this post


Link to post
Share on other sites
You're currently checking both current and previous keyboard for the key being down; is that what you meant to do? Usually you'd want your check to include it being down in previous and up in current (meaning it just got released) or up in previous and down in current (meaning it was just pressed).




In case that's not the solution/problem you're having, what framework is this in (xna, winforms, etc)?

Share this post


Link to post
Share on other sites
That's definitely XNA, unless something else has those exact same methods and enums.

If the code shown so far is the only problem, it should work if you hold down the enter key for the span of two frames. If that doesn't work, the code shown is not the main problem and you'll need to show us all the lines of code (with context, so you may as well paste the entire methods) where those two key states are being assigned values.

Share this post


Link to post
Share on other sites

This topic is 2321 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this