0

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

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

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

2 回答 2

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()用于比较,它返回一个布尔值,告诉您元素是否相等,但看起来您正在使用它进行分配或其他事情。

你在找类似的东西吗

else 
    selected = 0;

代替

else selected.Equals(0);
于 2013-05-14T14:29:01.443 回答
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));
    }
于 2013-05-14T18:19:39.390 回答