简体   繁体   English

XNA C#键盘阵列问题

[英]XNA C# Issue with Keyboard Array

I've started making a Player class for my game, so due to the amount of controls for the game I used the Key's array into a switch to make it simple. 我已经开始为我的游戏制作Player类,因此由于游戏的控件数量众多,我将Key的数组放入了一个开关中以使其变得简单。 It works properly with all keys except for one. 它适用于除一个以外的所有键。

When I press Space Bar and The Arrow Keys, it only detects one of the arrow keys and the space bar at the same time. 当我按下空格键和箭头键时,它只能同时检测到箭头键和空格键之一。 But it only occurs with the space bar. 但是它仅在空格键时发生。 I was trying to solve this but I commented the space part and it does the same. 我试图解决这个问题,但是我评论了空格部分,它也做了同样的事情。

It doesn't happen if i use the isDown method. 如果我使用isDown方法,则不会发生。

Here is the code: 这是代码:

for (int Key = 0; Key < Keyboard.GetState().GetPressedKeys().Count(); Key++)
{
    switch (Keyboard.GetState().GetPressedKeys()[Key])
    {
        case Keys.Up:
            Move(Direction.Up);
            Console.WriteLine("Up");
            break;
        case Keys.Down:
            Move(Direction.Down);
            Console.WriteLine("Down");
            break;
        case Keys.Left:
            Move(Direction.Left);
            Console.WriteLine("Left");
            break;
        case Keys.Right:
            Move(Direction.Right);
            Console.WriteLine("Right");
            break;
        case Keys.LeftShift:
            if (!isShifting)
                isShifting = true;
            break;
        case Keys.A:
            if (!oldKeyboardState.IsKeyDown(Keys.A))
                CastSkill(1);
            break;
        case Keys.Space:
            shootMgr.Shoot();
            break;
    }
}

I would personally do it in ifs instead of your for and switch. 我会亲自在ifs而不是for和switch中进行操作。

var keys = Keyboard.GetState();

if (keys.IsKeyDown(Keys.Up) {
    Move(Direction.Up);
    Console.WriteLine("Up");
}
if (keys.IsKeyDown(Keys.Down) {
    ...
}

But if you wanted to do it with a loop and switch, what happens when you switch it around to: 但是,如果您想通过循环和切换来完成此操作,将其切换到以下位置时会发生什么:

var keys = Keyboard.GetState().GetPressedKeys();
for (var i = 0; i < keys.Count(); i++) {
    switch (keys[i]) {
        case: Keys.Up:
            ...
            break;
        case: Keys.Down:
            ...
            break;
    }
}

I believe your problem is coming from calling and receiving a new array from GetPressedKeys() each iteration of your loop. 我相信您的问题来自循环的每次迭代从GetPressedKeys()调用和接收新数组。

How many keys do u press at the same time. 您要同时按下多少键。 If you are pressing to many at the same time u will get 'Keyboard ghosting': http://www.microsoft.com/appliedsciences/antighostingexplained.mspx 如果您同时按下多个按钮,您将得到“键盘重影”: http : //www.microsoft.com/appliedsciences/antighostingexplained.mspx

Now if that's not the case, I would like to see what your Move(Direction.Up) does. 现在,如果不是这种情况,我想看看您的Move(Direction.Up)做什么。

I would personally not use a for switch but: 我个人不会使用for开关,而是:

if (Keyboard.GetState().IsKeyDown(Keys.D))
{
     velocity.Y = 0f;
     velocity.X = 2f;                
}

Put this is in the update from your Player class(in case you didn't know :S) 将其放在Player类的更新中(以防您不知道:S)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM