开发者

How to take user input like name and gender in xna?

开发者 https://www.devze.com 2023-02-04 04:27 出处:网络
I am making game in xna and now I need to take some input like name and gender. It would be much easier if I was using C# but I don\'t 开发者_开发技巧know how to do this here.You are using C# when you

I am making game in xna and now I need to take some input like name and gender. It would be much easier if I was using C# but I don't 开发者_开发技巧know how to do this here.


You are using C# when you're using XNA. The XNA framework is just a set of dlls that you program against using C#.

As for taking input, you can either code your own controls or use some existing libraries. Both options have their pros and cons.

We have a FAQ over on the XNA forums called "What GUI Systems are there for the XNA framework" that would probably be a useful read for you -> http://forums.create.msdn.com/forums/t/15274.aspx


Use an existing user interface library such as xWinForms. You can find more options in the UI library for XNA question.


If I were doing this, I would listen for some keys and then the "Enter" buttons. I would then repeat that as many times as I needed. The code for it is below.

KeyboardState key;
OldKeyboardState oldKey;
String input;
//Puts final input in this
String finalIn;
String finalIn2;
String finalIn3;

protected override void Update(GameTime gameTime)
{
    key = Keyboard.GetState();
    //Do for all acceptable characters
    if (key.IsKeyDown(Keys.A) && key.IsKeyDown(Keys.RightShift) && oldKey.IsKeyUp(Keys.A) && oldKey.IsKeyUp(Keys.RightShift)) input = input + "A";
    else if (key.IsKeyDown(Keys.A) && oldKey.IsKeyUp(Keys.A) input = input + "a";
    //Etc.
    //Finalize input when enter is pressed
    if(key.IsKeyDown(Keys.Enter) && oldKey.IsKeyUp(Keys.Enter))
    {
        finalIn = input;
        input = "";
    }
    //Finalize input when enter is pressed for second input
    if(key.IsKeyDown(Keys.Enter) && oldKey.IsKeyUp(Keys.Enter) && finalIn != "")
    {
        finalIn2 = input;
        input = "";
    }
    //Etc.
    //At end set oldKey = key, so we have the current one and the old one
    oldKey = key;
}
protected override void Draw(GameTime gameTime)
{
    GraphicsDevice.Clear(Color.CornflowerBlue);
    //If desired, add in a draw string to show user what is being inputed
    base.Draw(gameTime);
}


It's easier to implement your own controlls. Use spritebatch and register key strokes in a string buffer, then show it on the screen using spriebatch.DrawString(..)

0

精彩评论

暂无评论...
验证码 换一张
取 消