5

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.

2 Answers2

1

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));
}
rhughes
  • 9,257
  • 11
  • 59
  • 87
  • @hunteroatway17 No problem. Don't forget to mark the correct answer so the whole SO community can benefit. – rhughes Mar 23 '13 at 14:40
-1

I think its because

 if (CheckKeyboard(Keys.Up))
    {
        if (selected > 0)
        {
            selected--;
            // This loops executes so quick before you release button.
            // Make changes here to stop the loop if the button is pressed and loop
            // executed once.(May be) just **return;** would do ?
        }
    }
    if (CheckKeyboard(Keys.Down))
    {
        if (selected < buttonList.Count - 1)
        {
            selected++;
            // This loops executes so quick before you release button.
            // Make changes here to stop the loop if the button is pressed and loop
            // executed once.(May be) just **return;** would do ?
        }
    }
Sakthivel
  • 1,890
  • 2
  • 21
  • 47