Nov 032013

This time, lets see how to use the keyboard input to move a texture around since the update and draw event have no secrets for us anymore 🙂
And from now on, we’ll use XNA content files (see previous article).

Lets use the KeyboardState class and its GetState method.
And upon specific keypress (if newState.IsKeyDown(Keys.Right) …) lets update the X and Y of our texture.

As always, the full project : xna_demo_3 .

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

Public Class Game
Inherits Microsoft.Xna.Framework.Game
'Fields in our game graphic manager etc'
Dim graphics As GraphicsDeviceManager
Dim font1 As SpriteFont
Dim msg As String
Dim spriteBatch As SpriteBatch
'Texture that we will render'
Private mTexture As Texture2D
'Set the coordinates to draw the sprite at'
Private spritePos As Vector2 = Vector2.Zero

Public Sub New()
graphics = New GraphicsDeviceManager(Me)

End Sub

Protected Overrides Sub Initialize()
'TODO: Add your initialization logic here'
msg = "hello world"
End Sub
Protected Overrides Sub LoadContent()
' TODO: use this.Content to load your game content here'
' Create a new SpriteBatch, which can be used to draw textures.'
spriteBatch = New SpriteBatch(GraphicsDevice)
'Loading the texture'
mTexture = Content.Load(Of Texture2D)("bille")
'lets create a font
font1 = Content.Load(Of SpriteFont)("myfont")

End Sub
Protected Overrides Sub UnloadContent()
'TODO: Unload any non ContentManager content here'
End Sub
Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'Allows the game to exit'
If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
End If
'TODO: Add your update logic here'



End Sub
Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'TODO: Add your drawing code here'
'Draw the sprite'
spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)
spriteBatch.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White)
spriteBatch.Draw(mTexture, spritePos, Color.White)
End Sub

Private Sub updateinput(ByVal gameTime As GameTime)
Dim newState As KeyboardState
newState = Keyboard.GetState

If (newState.IsKeyDown(Keys.Right)) Then
spritePos.X = spritePos.X + 10
msg = "right " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Left)) Then
spritePos.X = spritePos.X - 10
msg = "left " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Up)) Then
spritePos.Y = spritePos.Y - 10
msg = "up " & spritePos.X & "," & spritePos.Y
End If

If (newState.IsKeyDown(Keys.Down)) Then
spritePos.Y = spritePos.Y + 10
msg = "down " & spritePos.X & "," & spritePos.Y
End If
End Sub
End Class

 Posted by at 23 h 09 min

 Leave a Reply

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>



Ce site utilise Akismet pour réduire les indésirables. En savoir plus sur comment les données de vos commentaires sont utilisées.