5

我最近开始使用 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));
    }

我需要有人帮我把它放慢到合理的速度。

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

4

2 回答 2

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));
}
于 2013-03-23T05:21:03.013 回答
-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 ?
        }
    }
于 2013-03-23T05:35:45.527 回答