简体   繁体   English

C#XNA键盘输入问题。 代码总是返回false

[英]C# XNA keyboard input issue. Code always returns false

I'm attempting to implement a way for the player to change controls, and then save that data to a text file. 我正在尝试为播放器实现一种更改控件的方法,然后将该数据保存到文本文件中。

Only my InputManager class uses the Xna.Input.Keys enum, everything else getting keyboard input passes in a string. 只有我的InputManager类使用Xna.Input.Keys枚举,其他所有获取键盘输入的内容都以字符串形式传递。 The InputManager contains a dictionary with the key being the dynamic string passed in from outside, and the value is the corresponding key in the Input.Keys enum. InputManager包含一个词典,其键是从外部传入的动态字符串,值是Input.Keys枚举中的对应键。

All this is loaded in from a file (keyconfig.cfg, just a standard text file). 所有这些都从文件(keyconfig.cfg,仅是标准文本文件)中加载。
I've tried modifying the code in various ways, but I can't seem to get it to work properly. 我试图以各种方式修改代码,但似乎无法使其正常工作。 The problem appears to be coming this particular method of mine, which is a part of the InputManager: 问题似乎出在我的这种特定方法上,它是InputManager的一部分:

public bool IsButtonPressed(string keyString)
    {
        if (keyState.IsKeyDown(KeyFromString(keyString)) && keyState_old.IsKeyUp(KeyFromString(keyString)))
           return true;
        return false;
    }
  • KeyFromString casts the string to a Keys type KeyFromString将字符串强制转换为Keys类型
  • keyState & keyState_old are KeyboardState instances (_old being an image of last frame) keyState和keyState_old是KeyboardState实例(_old是最后一帧的图像)
  • The KeyboardState instances are updated every frame through an Update() KeyboardState实例通过Update()每帧更新一次

I'm setting my keyState & keyState_old variables within the Update() of the InputManager like so: 我在InputManager的Update()中设置keyState和keyState_old变量,如下所示:

public void Update(GameTime gameTime)
{
    keyState = Keyboard.GetState();
    mouseState = Mouse.GetState();
    //Updates some other stuff within the class here, unrelated to keyboard input
    keyState_old = keyState;
}

I'm absolutely stuck on this, apologies if any of this is unclear, my english is not the best. 我绝对会坚持这一点,如果其中任何一个不清楚,我深表歉意,我的英语不是最好的。 Thanks in Advance. 提前致谢。

It is probably safer to assign keyState_old before reading the new state to avoid overwriting it 为避免覆盖新状态,在读取新状态之前分配keyState_old可能更安全

public void Update(GameTime gameTime)
{
    keyState_old = keyState;  // Do this first

    keyState = Keyboard.GetState();  // Then this
    mouseState = Mouse.GetState();
    //Updates some other stuff within the class here, unrelated to keyboard input
}

Otherwise if a call is made to IsButtonPressed is made from outside the InputManager.Update method, the current and old key states will be the same value 否则,如果从InputManager.Update方法外部调用IsButtonPressed,则当前键状态和旧键状态将为相同的值

keyState = Keyboard.GetState();
keyState_old = keyState;

This right here is your problem. 这是您的问题。 You get the new state, and put it into keyState , and then you put keyState into keyState_old ... so you have two copies of the same state. 获得新状态,并将其放入keyState ,然后将keyState放入keyState_old ...,因此您具有相同状态的两个副本。 Put these two lines together, rather than either side of that block of unrelated stuff, and do them the other way round. 将这两条线放在一起,而不是放在那无关紧要的东西的两侧,然后反过来做。 Save the old state, then get the new one. 保存旧状态, 然后获取新状态。

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

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