Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Main Menu navigation / keyboard input speed is too fast

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.

like image 560
hunteroatway17 Avatar asked Nov 25 '22 02:11

hunteroatway17


1 Answers

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));
}
like image 196
rhughes Avatar answered Nov 26 '22 16:11

rhughes