{"id":579,"date":"2013-11-04T22:57:29","date_gmt":"2013-11-04T21:57:29","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=579"},"modified":"2013-11-16T14:33:20","modified_gmt":"2013-11-16T13:33:20","slug":"vb-net-and-xna-article-5-mouse-input","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=579","title":{"rendered":"VB.Net and XNA : article 5 (mouse input)"},"content":{"rendered":"<p>A quick one : lets adapt the last example (VB.Net and XNA : Article 4) and lets illustrate how to use the mouse input.<br \/>\nHere our texture will move around as we move the mouse.<\/p>\n<p>Here are the project files : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_3.1.zip\">xna_demo_3.1<\/a><\/p>\n<p>And below the code to review. Look at the updatemouse method.<br \/>\n<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()<br \/>\n    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<\/p>\n<p>    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\")<br \/>\n    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<\/p>\n<p>    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'<br \/>\n        updatemouse()<br \/>\n        '<br \/>\n        MyBase.Update(gameTime)<br \/>\n    End Sub<\/p>\n<p>    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 updatemouse()<br \/>\n        Dim newState As MouseState<br \/>\n        newState = Mouse.GetState<br \/>\n        spritePos.X = newState.X<br \/>\n        spritePos.Y = newState.Y<br \/>\n        '<br \/>\n        msg = spritePos.X & \",\" & spritePos.Y<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-579-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna05.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna05.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna05.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>A quick one : lets adapt the last example (VB.Net and XNA : Article 4) and lets illustrate how to use the mouse input. Here our texture will move around as we move the mouse. Here are the project files : xna_demo_3.1 And below the code to review. Look at the updatemouse method. Imports Microsoft.Xna.Framework <a href='https:\/\/labalec.fr\/erwan\/?p=579' 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-579","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\/579","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=579"}],"version-history":[{"count":3,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions"}],"predecessor-version":[{"id":606,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/579\/revisions\/606"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=579"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=579"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=579"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}