我在xna製作遊戲,現在我需要輸入一些像姓名和性別的信息。如果我使用C#但會更容易,但我不知道如何在此處執行此操作。請儘快幫助如何在xna中像用戶名和性別一樣輸入用戶輸入?
1
A
回答
0
使用現有的用戶界面庫,如xWinForms。您可以在UI library for XNA問題中找到更多選項。
4
當您使用XNA時,您正在使用C#。 XNA框架只是您使用C#編程的一組dll。
至於輸入,你可以編碼自己的控件或使用一些現有的庫。兩種選擇都有其優點和缺點。
我們在有一個FAQ上稱爲XNA論壇「什麼GUI系統是那裏的XNA框架」,可能會成爲你的有用讀 - >http://forums.create.msdn.com/forums/t/15274.aspx
0
如果我這樣做,我會聽對於某些按鍵,然後是「Enter」按鈕。我會盡可能多地重複我所需要的。代碼如下。
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);
}
0
實現您自己的控件更容易。在字符串緩衝區中使用spritebatch並註冊擊鍵,然後使用spriebatch.DrawString(..)在屏幕上顯示它(...)