Nov 082013
 

This time lets scree how to make a scrolling in VB.Net and XNA.

Rather simple : the idea is to load a texture and to draw it into 2 rectangles, next to each other, and to update the rectangles position.

The project files : XNA_DEMO_8


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
' // 1. Load a Texture2D and SpriteBatch as normal.
Dim texture1 As Texture2D
Dim spriteBatch As SpriteBatch
' // 2. Create two rectangles for the texture.
Dim rectangle1 As Rectangle
Dim rectangle2 As Rectangle
'// 3. step for the scrolling
Dim istep As Integer = 5
'// some text we will display on screen
Dim font1 As SpriteFont
Dim msg As String

Public Sub New()
graphics = New GraphicsDeviceManager(Me)
Window.Title = "lets Scroll!"
graphics.PreferredBackBufferWidth = 800
graphics.PreferredBackBufferHeight = 410
End Sub

Protected Overrides Sub Initialize()
'// 3a. rectangle1 and 2 should be equal to the
'// dimension of the texture.
rectangle1 = New Rectangle(0, 0, 2048, 410)
'// 3b. Position rectangle2 to the immediate right of rectangle1.
rectangle2 = New Rectangle(2048, 0, 2048, 410)
MyBase.Initialize()
End Sub

Protected Overrides Sub LoadContent()
MyBase.LoadContent()
spriteBatch = New SpriteBatch(GraphicsDevice)
texture1 = Content.Load(Of Texture2D)("back1")
'lets create a font
font1 = Content.Load(Of SpriteFont)("myfont")
End Sub

Protected Overrides Sub UnloadContent()
MyBase.UnloadContent()
End Sub

Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)
'// 5a. This is where the quote unquote "magic" happens.
'// This is a simple bounds check. If the right edge of
'// rectangle1 is offscreen to the left, you move it to
'// the right side of rectangle2.
If (rectangle1.X + texture1.Width <= 0) Then rectangle1.X = rectangle2.X + texture1.Width ' // You repeat this check for rectangle2. If (rectangle2.X + texture1.Width <= 0) Then rectangle2.X = rectangle1.X + texture1.Width '// 6. Otherwise we incrementally move it to the left. '// You can swap out X for Y if you instead want incremental '// vertical scrolling. Dim key As KeyboardState key = Keyboard.GetState If key.IsKeyDown(Keys.Add) Then istep = istep + 1 If key.IsKeyDown(Keys.Subtract) Then istep = istep - 1 rectangle1.X -= istep rectangle2.X -= istep msg = "step= " & istep.ToString ' MyBase.Update(gameTime) End Sub Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime) '// 4. Load the SpriteBatch as normal. '// load the same texture into two different rectangles. spriteBatch.Begin() spriteBatch.Draw(texture1, rectangle1, Color.White) spriteBatch.Draw(texture1, rectangle2, Color.White) spriteBatch.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White) spriteBatch.End() MyBase.Draw(gameTime) End Sub End Class

 Posted by at 19 h 24 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>

(required)

(required)

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