XNA - 冻结菜单错误

时间:2013-05-14 14:15:56

标签: c# xna

当我单击向下箭头时,它会卡在第二个选择上,向上或向下不再起作用,我该如何解决?

第二个问题:更改菜单项时如何防止冻结?当我更改菜单项时,它冻结了第二个选择。这是关于这个问题的代码;

keyboard = Keyboard.GetState();
mouse = Mouse.GetState();

    if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))
    {
        if (selected > 0) selected--;
        else selected.Equals(buttonList.Count - 1);
    }

    if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))
    {
        if (selected < buttonList.Count - 1) selected++;
        else selected.Equals(0);
    }

    prevMouse = mouse;
    prevKeyboard = keyboard;
}

2 个答案:

答案 0 :(得分:2)

你的if陈述没有多大意义,而且它们都完全相同:

if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Down))

但如果它们是相同的,人们会认为你只是将它们组合成一个。

看起来您正在尝试使用以下范例

if (keyboard.IsKeyUp(Keys.Down) && prevKeyboard.IsKeyDown(Keys.Down))

...

if (keyboard.IsKeyUp(Keys.Up) && prevKeyboard.IsKeyDown(Keys.Up))

我注意到的另一个奇怪之处在于你使用Equals()方法的方式。

你没有用它的返回值做任何事情。

Equals()用于比较,它返回一个bool,告诉你元素是否相等,但看起来你正在使用它进行赋值或其他东西。

你正在寻找像

这样的东西吗?
else 
    selected = 0;

而不是

else selected.Equals(0);

答案 1 :(得分:0)

您修改后的原始代码对我来说很好。这里有修改:

    public void Update(GameTime gameTime)
    {
        keyboard = Keyboard.GetState();
        mouse = Mouse.GetState();

        if (CheckKeyboard(Keys.Up))
        {
            if (selected > 0) selected--;
            else{selected = buttonList.Count - 1;}
        }

        if (CheckKeyboard(Keys.Down))
        {
            if (selected < buttonList.Count - 1) selected++;
            else {selected = 0;}
        }

        prevMouse = mouse;
        prevKeyboard = keyboard;
    }

    public bool CheckMouse()
    {
        return (mouse.LeftButton == ButtonState.Pressed && prevMouse.LeftButton == ButtonState.Released);
    }

    public bool CheckKeyboard(Keys key)
    {
        //Here is the only thing that needed to be changed, based on your original post
        //you were checking if both keys were down originally, meaning it was always 
        // true while a button was pressed. if the previous keyboard was down,
        //and the current keyboard is up, it means the button was released.
        return (keyboard.IsKeyUp(key) && prevKeyboard.IsKeyDown(key));
    }