Nov 292013
 

There are times where you need to get text input from the player like his name, etc.

Although this is possible to do it with XNA only, it is a bit of a hassle : you need to put a timer between each keystroke, etc and result is not perfect.

Lets use Nuclex framework.
Yes the same Nuclex we used in a previous article for gamepad inputs.

Code is rather simpler : we add a reference to nuclex framework, add an event handler and finally draw our string on screen. As simple as that !

Source is there : .XNA_DEMO_24
See vide lower in this article.


Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Audio
Imports Microsoft.Xna.Framework.Content
Imports Microsoft.Xna.Framework.Media
Imports Microsoft.Xna.Framework.Input
Imports Nuclex.Input

Public Class Game
Inherits Microsoft.Xna.Framework.Game
Dim graphics As GraphicsDeviceManager
Dim SpriteBatch As SpriteBatch
Dim input As InputManager
Dim myfont As SpriteFont
Dim msg As String
Dim dummyTexture As Texture2D
Public Sub New()
graphics = New GraphicsDeviceManager(Me)
input = New InputManager(Services, Window.Handle)
Components.Add(input)
Window.Title = "Test"
End Sub
Protected Overrides Sub Initialize()
MyBase.Initialize()
AddHandler input.GetKeyboard.CharacterEntered, AddressOf keyboardCharacterEntered
End Sub
Protected Overrides Sub LoadContent()
SpriteBatch = New SpriteBatch(GraphicsDevice)
myfont = Content.Load(Of SpriteFont)("xnb\myfont")
MyBase.LoadContent()
End Sub
Private Sub keyboardCharacterEntered(character As Char)
If Asc(character) = 8 Then
If msg.Length > 0 Then msg = msg.Remove(msg.Length - 1)
Else
If Asc(character) >= 32 And Asc(character) <= 126 Then msg += character End If End Sub Protected Overrides Sub UnloadContent() MyBase.UnloadContent() End Sub Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime) MyBase.Update(gameTime) End Sub Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime) GraphicsDevice.Clear(Color.CornflowerBlue) SpriteBatch.Begin() If msg <> "" Then SpriteBatch.DrawString(myfont, msg, New Vector2(10, 10), Color.White)
SpriteBatch.End()
MyBase.Draw(gameTime)
End Sub
End Class

 Posted by at 15 h 00 min

Poster un Commentaire

avatar
  S’abonner  
Notifier de