1

我是 XNA 的新手。我正在制作一个需要关卡编辑器的游戏。我需要一个文本框来获取关卡名称及其描述。所以我写了这样的代码:

if (isSaveBox)
        {
            KeyboardState ks = Keyboard.GetState();
            Keys[] keys = ks.GetPressedKeys();
            Keys tempKey = Keys.None;

            foreach (Keys key in keys)
            {
                Keys currentKey = key;
                if (ks.IsKeyUp(lastKey))
                {
                    string toadd = key.ToString();
                    if (!(key == Keys.None) && key != Keys.Space && key != Keys.Back && key != Keys.Enter)
                    {
                        levelName += toadd;
                    }
                    else if (key == Keys.Space)
                    {
                        levelName += " ";
                    }
                    else if (key == Keys.Back)
                    {
                        levelName.Remove(levelName.Length - 1);
                        lastKey = currentKey;
                    }
                }
                if (currentKey != Keys.None && ks.IsKeyDown(currentKey))
                {
                    tempKey = currentKey;
                }
            }

            lastKey = tempKey;
            message = "Save level" + "\n" + "Enter - yes / Esc - no" + "\n" + levelName;

但是当我尝试输入一些字母时,它们不会被添加到levelName字符串中。有人可以帮我吗?

4

2 回答 2

3

据我所知,您的代码工作正常。我玩弄了它并添加了一些改进。你不需要一些东西,我做到了,所以当你拿着钥匙时,它不会那么快地发送垃圾邮件。我会添加对其他键(如符号和大写字母)的支持,但它是一个相当长的 switch 语句,因此您可以查看这篇文章并轻松实现它。

我添加了更多变量,您所有与文本框相关的内容现在应该读作

    Keys[] lastKeys;
    KeyboardState lastKeyboardState;

    public double timer;

    public string levelName;

如果需要,您还可以将currentKeyboardState变量添加为全局变量。

现在对于代码,我将其拆分为 2 种方法,以防止必须复制代码两次

if (isSaveBox)
{
    //Get the current keyboard state and keys that are pressed
    KeyboardState keyboardState = Keyboard.GetState();
    Keys[] keys = keyboardState.GetPressedKeys();

    foreach (Keys currentKey in keys)
    {
        if (currentKey != Keys.None)
        {
            //If we have pressed the same key twice, wait atleast 125ms before adding it again
            if (lastKeys.Contains(currentKey))
            {
                if ((gameTime.TotalGameTime.TotalMilliseconds - timer > 125))
                    HandleKey(gameTime, currentKey);
            }
            //If we press a new key, add it
            else if (!lastKeys.Contains(currentKey))
                HandleKey(gameTime, currentKey);
        }
    }

    //Save the last keys and pressed keys array
    lastKeyboardState = keyboardState;
    lastKeys = keys;
}

以及重置计时器并实际找出字符串的方法(这是您添加移位和符号等代码的地方)

private void HandleKey(GameTime gameTime, Keys currentKey)
{
    string keyString = currentKey.ToString();
    if (currentKey == Keys.Space)
        levelName += " ";
    else if ((currentKey == Keys.Back || currentKey == Keys.Delete) && levelName.Length > 0)
        levelName = levelName.Remove(levelName.Length - 1);
    else if (currentKey == Keys.Enter)
        return;
    else
        levelName += keyString;
    //Set the timer to the current time
    timer = gameTime.TotalGameTime.TotalMilliseconds;
}

您还可以使用 aStringBuilder进行进一步优化。

如果您想继续编写自己的 UI,请继续。我仍然会推荐使用 UI 库(我使用新恢复的Neoforce Controls ),但是这里有一个更大的讨论。

于 2013-06-26T14:32:17.927 回答
1

我推荐你使用一个 XNA GUI 框架,它包含表单元素、场景管理器和其他有用的东西。

这里那里

于 2013-06-26T12:57:04.590 回答