C# XNA 游戏中的文本框 [英] Textbox in a C# XNA Game

查看:32
本文介绍了C# XNA 游戏中的文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm new to XNA. I'm making a game which needs level editor. I need a textbox to get a level name and its description. So I wrote a code like this:

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 字符串中.有人可以帮我解决这个问题吗?

But when I try to type some letters they won't be added to the levelName string. Can somebody help me with this?

推荐答案

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

As far as I can tell your code works fine. I played around with it and added some improvements. You didn't need some stuff and I made it so when you hold a key it doesn't spam so fast. I would have added support for other keys like symbols and caps, but it's quite a long switch statement for all that, so you can take a look at this article and implement it easily.

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

I added a few more variable, all your textbox related stuff should now read as

    Keys[] lastKeys;
    KeyboardState lastKeyboardState;

    public double timer;

    public string levelName;

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

You can also add your currentKeyboardState variable as a global variable if you want.

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

Now for the code, I split it into 2 methods to prevent having to copy the code twice

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

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

And the method to reset the timer and actually figure out the strings (this is where you would add code for shift and symbols, etc)

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

您也可以使用 StringBuilder 进行进一步优化.

You could also use a StringBuilder for further optimization.

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

If you want to continue writing your own UI, go ahead. I still would recommend using a UI library (I use the newly revived Neoforce Controls) but there is a larger discussion for that over here.

这篇关于C# XNA 游戏中的文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆