Reputation: 308
When I click the down arrow it gets stuck on the second selection and up or down no longer work, how would I fix that?
Second Question: How to prevent freezing when changing menu items? When I change menu items, it freezes of the second selection. Here is the code concerning this question;
keyboard = Keyboard.GetState();
mouse = Mouse.GetState();
if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))
{
if (selected > 0) selected--;
else selected.Equals(buttonList.Count - 1);
}
if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))
{
if (selected < buttonList.Count - 1) selected++;
else selected.Equals(0);
}
prevMouse = mouse;
prevKeyboard = keyboard;
}
Upvotes: 0
Views: 74
Reputation: 1297
The original code you had, after being modified worked fine for me. Here it is with modifications:
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
mouse = Mouse.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0) selected--;
else{selected = buttonList.Count - 1;}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1) selected++;
else {selected = 0;}
}
prevMouse = mouse;
prevKeyboard = keyboard;
}
public bool CheckMouse()
{
return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
}
public bool CheckKeyboard(Keys key)
{
//Here is the only thing that needed to be changed, based on your original post
//you were checking if both keys were down originally, meaning it was always
// true while a button was pressed. if the previous keyboard was down,
//and the current keyboard is up, it means the button was released.
return (keyboard.IsKeyUp(key) && prevKeyboard.IsKeyDown(key));
}
Upvotes: 0
Reputation: 31204
Your if
statements don't make very much sense, and they're both exactly the same:
if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))
but if they were intended to be the same, one would think that you'd just just combine them into one.
It appears like you're trying to use the following paradigm
if (keyboard.IsKeyUp(Keys.Down) && prevKeyboard.IsKeyDown(Keys.Down))
...
if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Up))
another oddity I notice is the way you're using the Equals()
method.
You're not doing anything with it's return value.
Equals()
is used for comparison and it returns a bool telling you whether the elements are equal, but It looks like you're using it for assignment or something.
are you looking for something like
else
selected = 0;
instead of
else selected.Equals(0);
Upvotes: 2