1) If you do this:
KeyboardState newState = Keyboard.GetState();
while (newState.IsKeyDown(Keys.Down)) {
/* ... */
}
you will query the keyboard state only once (in the first line) and then keep checking the saved state, which never changes. Thus the endless loop.
2) You've got several lines like
if(selc == 0) { /* ... */ selc = 0; }. Setting
selc to 0 only when it is 0... doesn't really make sense :)
3) Also, you're checking for the
enter key inside your
while loop, which is only entered when the
down key is pressed. Then means you can only select a menu item if you press
enter while you're also pressing
down. In addition, as described in (1), you're only checking the keyboard state once at the top, so the user would have to press
enter and
down at exactly the same time, which is next to impossible.
You should remove the while loop entirely, otherwise the game will stop drawing because it's looping with 100% CPU usage in that loop waiting for you to release the
down key.
Instead
- Just check whether a key is down once per frame
- If it is down, set a flag to remember it is down
- Only check again when the key has been released in the meantime
In code:
void Updatemenu(GameTime gameTime) {
KeyboardState newState = Keyboard.GetState();
if(newState.IsKeyDown(Keys.Down)) {
if(!this.downPressed) {
++this.selc;
this.downPressed = true;
}
} else { // down is NOT pressed
this.downPressed = false;
}
if(newState.IsKeyDown(Keys.Enter)) {
switch(this.selc) {
case 0: { /* code for first menu item */ break; }
case 0: { /* code for second menu item */ break; }
/* ... */
}
}
}-Markus-
Check out my
website and
blog for some interesting articles and useful utility classes!
Nuclex Framework:
threaded particles,
skinnable GUI,
vector fonts,
texture atlasses and lots more.
WiX XNA Installer: Professional-looking MSI installer template for XNA games.