{"id":574,"date":"2013-11-03T23:09:50","date_gmt":"2013-11-03T22:09:50","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=574"},"modified":"2014-03-01T15:21:07","modified_gmt":"2014-03-01T14:21:07","slug":"vb-net-and-xna-article-4-moving-a-texture","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=574","title":{"rendered":"VB.NET and XNA : Article 4 (keyboard input)"},"content":{"rendered":"<p>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 \ud83d\ude42<br \/>\nAnd from now on, we&rsquo;ll use XNA content files (see previous article).<\/p>\n<p>Lets use the KeyboardState class and its GetState method.<br \/>\nAnd upon specific keypress (if newState.IsKeyDown(Keys.Right) &#8230;) lets update the X and Y of our texture.<\/p>\n<p>As always, the full project : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_3.zip\">xna_demo_3<\/a> .<\/p>\n<p><code><br \/>\nImports Microsoft.Xna.Framework<br \/>\nImports Microsoft.Xna.Framework.Graphics<br \/>\nImports Microsoft.Xna.Framework.Audio<br \/>\nImports Microsoft.Xna.Framework.Content<br \/>\nImports Microsoft.Xna.Framework.Media<br \/>\nImports Microsoft.Xna.Framework.Input<\/p>\n<p>Public Class Game<br \/>\n    Inherits Microsoft.Xna.Framework.Game<br \/>\n    'Fields in our game graphic manager etc'<br \/>\n    Dim graphics As GraphicsDeviceManager<br \/>\n    '<br \/>\n    Dim font1 As SpriteFont<br \/>\n    Dim msg As String<br \/>\n    '<br \/>\n    Dim spriteBatch As SpriteBatch<br \/>\n    '<br \/>\n    'Texture that we will render'<br \/>\n    Private mTexture As Texture2D<br \/>\n    'Set the coordinates to draw the sprite at'<br \/>\n    Private spritePos As Vector2 = Vector2.Zero<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n        'graphics.ToggleFullScreen()<\/p>\n<p>    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        'TODO: Add your initialization logic here'<br \/>\n        MyBase.Initialize()<br \/>\n        msg = \"hello world\"<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub LoadContent()<br \/>\n        ' TODO: use this.Content to load your game content here'<br \/>\n        MyBase.LoadContent()<br \/>\n        ' Create a new SpriteBatch, which can be used to draw textures.'<br \/>\n        spriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        'Loading the texture'<br \/>\n        mTexture = Content.Load(Of Texture2D)(\"bille\")<br \/>\n        'lets create a font<br \/>\n        font1 = Content.Load(Of SpriteFont)(\"myfont\")<\/p>\n<p>    End Sub<br \/>\n    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n        'TODO: Unload any non ContentManager content here'<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        'Allows the game to exit'<br \/>\n        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then<br \/>\n            Me.Exit()<br \/>\n        End If<br \/>\n        'TODO: Add your update logic here'<\/p>\n<p>        updateinput(gameTime)<\/p>\n<p>        MyBase.Update(gameTime)<\/p>\n<p>    End Sub<br \/>\n    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        GraphicsDevice.Clear(Color.CornflowerBlue)<br \/>\n        'TODO: Add your drawing code here'<br \/>\n        'Draw the sprite'<br \/>\n        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)<br \/>\n        spriteBatch.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White)<br \/>\n        spriteBatch.Draw(mTexture, spritePos, Color.White)<br \/>\n        spriteBatch.End()<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<\/p>\n<p>    Private Sub updateinput(ByVal gameTime As GameTime)<br \/>\n        Dim newState As KeyboardState<br \/>\n        newState = Keyboard.GetState<\/p>\n<p>        If (newState.IsKeyDown(Keys.Right)) Then<br \/>\n            spritePos.X = spritePos.X + 10<br \/>\n            msg = \"right \" & spritePos.X & \",\" & spritePos.Y<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Left)) Then<br \/>\n            spritePos.X = spritePos.X - 10<br \/>\n            msg = \"left \" & spritePos.X & \",\" & spritePos.Y<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Up)) Then<br \/>\n            spritePos.Y = spritePos.Y - 10<br \/>\n            msg = \"up \" & spritePos.X & \",\" & spritePos.Y<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Down)) Then<br \/>\n            spritePos.Y = spritePos.Y + 10<br \/>\n            msg = \"down \" & spritePos.X & \",\" & spritePos.Y<br \/>\n        End If<br \/>\n    End Sub<br \/>\nEnd Class<br \/>\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-574-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna04.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna04.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna04.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 \ud83d\ude42 And from now on, we&rsquo;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) <a href='https:\/\/labalec.fr\/erwan\/?p=574' class='excerpt-more'>[&#8230;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[34,35],"tags":[],"class_list":["post-574","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-xna","category-34-id","category-35-id","post-seq-1","post-parity-odd","meta-position-corners","fix"],"_links":{"self":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/574","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=574"}],"version-history":[{"count":5,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions"}],"predecessor-version":[{"id":604,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/574\/revisions\/604"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=574"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=574"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=574"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}