I recently started to make video games using the XNA game studio 4.0. I have made a main menu with 4 sprite fonts using a button list. They change color from White to Yellow when I press the up and down arrows.
My problem is that when I scroll through it goes from the top font to the bottom font really fast and goes straight to the last one. I am unsure why this is? Is it because I am putting it in the update method and it is calling it every 60 seconds or so?
Here is my code for when I press the arrow keys.
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0)
{
selected--;
}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1)
{
selected++;
}
}
keyboard = prevKeyboard;
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key));
}
I need someone to help me slow it down to a reasonable speed.
If you could help me it would be greatly appreciated.
I think the issue is because you are not setting prevKeyboard
correctly.
Try this:
public void Update(GameTime gameTime)
{
keyboard = Keyboard.GetState();
if (CheckKeyboard(Keys.Up))
{
if (selected > 0)
{
selected--;
}
}
if (CheckKeyboard(Keys.Down))
{
if (selected < buttonList.Count - 1)
{
selected++;
}
}
prevKeyboard = keyboard; // <=========== CHANGE MADE HERE
}
public bool CheckKeyboard(Keys key)
{
return (keyboard.IsKeyDown(key) && prevKeyboard.IsKeyUp(key));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With