主菜单导航/键盘输入速度太快

时间:2013-03-23 04:50:52

标签: c# input keyboard xna-4.0

我最近开始使用XNA游戏工作室4.0制作视频游戏。我使用按钮列表制作了一个包含4种精灵字体的主菜单。当我按下向上和向下箭头时,它们会从白色变为黄色。

我的问题是,当我滚动它时,从顶部字体到底部字体非常快,直接到最后一个字体。我不确定为什么会这样?是因为我将它放在更新方法中并且每隔60秒左右调用一次吗?

这是我按箭头键时的代码。

 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));
    }

我需要有人帮助我减速到合理的速度。

如果你能帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:1)

我认为问题是因为您没有正确设置prevKeyboard

试试这个:

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));
}

答案 1 :(得分:-1)

我认为是因为

 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 ?
        }
    }