{"id":589,"date":"2013-11-08T19:24:52","date_gmt":"2013-11-08T18:24:52","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=589"},"modified":"2013-11-17T12:58:26","modified_gmt":"2013-11-17T11:58:26","slug":"vb-net-and-xna-article-7-scrolling","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=589","title":{"rendered":"VB.Net and XNA : article 7 (Scrolling)"},"content":{"rendered":"<p>This time lets scree how to make a scrolling in VB.Net and XNA.<\/p>\n<p>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.<\/p>\n<p>The project files : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/XNA_DEMO_8.zip\">XNA_DEMO_8<\/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    ' \/\/ 1. Load a Texture2D and SpriteBatch as normal.<br \/>\n    Dim texture1 As Texture2D<br \/>\n    Dim spriteBatch As SpriteBatch<br \/>\n    ' \/\/ 2. Create two rectangles for the texture.<br \/>\n    Dim rectangle1 As Rectangle<br \/>\n    Dim rectangle2 As Rectangle<br \/>\n    '\/\/ 3. step for the scrolling<br \/>\n    Dim istep As Integer = 5<br \/>\n    '\/\/ some text we will display on screen<br \/>\n    Dim font1 As SpriteFont<br \/>\n    Dim msg As String<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n        Window.Title = \"lets Scroll!\"<br \/>\n        graphics.PreferredBackBufferWidth = 800<br \/>\n        graphics.PreferredBackBufferHeight = 410<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        '\/\/ 3a. rectangle1 and 2 should be equal to the<br \/>\n        '\/\/ dimension of the texture.<br \/>\n        rectangle1 = New Rectangle(0, 0, 2048, 410)<br \/>\n        '\/\/ 3b. Position rectangle2 to the immediate right of rectangle1.<br \/>\n        rectangle2 = New Rectangle(2048, 0, 2048, 410)<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub LoadContent()<br \/>\n        MyBase.LoadContent()<br \/>\n        spriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        texture1 = Content.Load(Of Texture2D)(\"back1\")<br \/>\n        'lets create a font<br \/>\n        font1 = Content.Load(Of SpriteFont)(\"myfont\")<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<br \/>\n        '\/\/ 5a. This is where the quote unquote \"magic\" happens.<br \/>\n        '\/\/ This is a simple bounds check. If the right edge of<br \/>\n        '\/\/ rectangle1 is offscreen to the left, you move it to<br \/>\n        '\/\/ the right side of rectangle2.<br \/>\n        If (rectangle1.X + texture1.Width <= 0) Then rectangle1.X = rectangle2.X + texture1.Width\n        ' \/\/ You repeat this check for rectangle2.\n        If (rectangle2.X + texture1.Width <= 0) Then rectangle2.X = rectangle1.X + texture1.Width\n        '\/\/ 6. Otherwise we incrementally move it to the left. \n        '\/\/ You can swap out X for Y if you instead want incremental \n        '\/\/ vertical scrolling.\n        Dim key As KeyboardState\n        key = Keyboard.GetState\n        If key.IsKeyDown(Keys.Add) Then istep = istep + 1\n        If key.IsKeyDown(Keys.Subtract) Then istep = istep - 1\n        rectangle1.X -= istep\n        rectangle2.X -= istep\n        msg = \"step= \" &#038; istep.ToString\n        '\n        MyBase.Update(gameTime)\n    End Sub\n\n    Protected Overrides Sub Draw(ByVal gameTime As Microsoft.Xna.Framework.GameTime)\n        '\/\/ 4. Load the SpriteBatch as normal.\n        '\/\/ load the same texture into two different rectangles.\n        spriteBatch.Begin()\n        spriteBatch.Draw(texture1, rectangle1, Color.White)\n        spriteBatch.Draw(texture1, rectangle2, Color.White)\n        spriteBatch.DrawString(font1, msg, New Vector2(20.0F, 20.0F), Color.White)\n        spriteBatch.End()\n        MyBase.Draw(gameTime)\n    End Sub\nEnd Class\n<\/code><\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-589-1\" width=\"695\" height=\"356\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna07.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna07.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna07.mp4<\/a><\/video><\/div>\n","protected":false},"excerpt":{"rendered":"<p>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 <a href='https:\/\/labalec.fr\/erwan\/?p=589' 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-589","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\/589","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=589"}],"version-history":[{"count":4,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions"}],"predecessor-version":[{"id":627,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/589\/revisions\/627"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=589"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=589"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=589"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}