我遇到了一个奇怪的问题,最明显的原因是我作为 XNA 游戏程序员的新身份,但这里有。我制作了一个 KeyboardManager 类,非常简单:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace CrazyCoders.XYZ.Framework
{
    public class Keyboard
    {
        public KeyboardState previousState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
        public KeyboardState newState;
        public void begin()
        {
            //Get the new state
            KeyboardState newState = Microsoft.Xna.Framework.Input.Keyboard.GetState();
        }
        public Keys[] detectKeyDowns()
        {
            return newState.GetPressedKeys().Except(previousState.GetPressedKeys()).ToArray();
        }
        public Keys[] detectKeyUps()
        {
            return previousState.GetPressedKeys().Except(newState.GetPressedKeys()).ToArray();
        }
        public void end()
        {
            //Save the new state for the next pass
            previousState = newState;
        }
    }
}
此类保存以前的键盘状态并在您调用 begin() 时采用新的键盘状态。然后使用 newState 和 previousState,detectKeyDowns 和 detectKeyUps 计算数组异常以返回最近真正按下或取消按下的内容。
问题是,事情不工作......
我试着自己添加
if (newState.IsKeyDown(Keys.I))
{
    Console.WriteLine(
}
在 newState 获取开始并中断它之后,它工作正常,例如,我看到我的“I”键被按下。但是当我调用detectKeyDowns 时,我似乎可以正确地除外。
你们看到我在这里做错了什么吗?