Nov 042013

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
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 updatemouse()
Dim newState As MouseState
newState = Mouse.GetState
spritePos.X = newState.X
spritePos.Y = newState.Y
msg = spritePos.X & "," & spritePos.Y
End Sub
End Class

 Posted by at 22 h 57 min

Poster un Commentaire

Me notifier des