{"id":706,"date":"2013-11-25T20:56:11","date_gmt":"2013-11-25T19:56:11","guid":{"rendered":"http:\/\/labalec.fr\/erwan\/?p=706"},"modified":"2013-11-25T20:59:54","modified_gmt":"2013-11-25T19:59:54","slug":"vb-net-and-xna-article-16-animating-a-sprite","status":"publish","type":"post","link":"https:\/\/labalec.fr\/erwan\/?p=706","title":{"rendered":"VB.Net and XNA : Article 16 &#8211; Animating a sprite"},"content":{"rendered":"<p>One thing that gives makes a game more dynamic is animated sprite.<br \/>\nIndeed, moving a fix shape all over the screen is nice. Moving an animated shape is better \ud83d\ude42<\/p>\n<p>Lets start with the below sprite sheet (yes it is link &#8211; hope no one will sue me for copyright infringement, this is only for educating purpose).<\/p>\n<p><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/spritesheet.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/spritesheet.png\" alt=\"spritesheet\" width=\"224\" height=\"32\" class=\"alignnone size-full wp-image-707\" \/><\/a><\/p>\n<p>What we have here is 8 28*32 frames.<br \/>\nFirst frame is at position x=0, second frame at position x=28, third frame at position x=56, etc&#8230; you get the idea.<br \/>\nWhich means that it we number our frame, we could easily get the position x=frame*28.<\/p>\n<p>Second trick is one overloaded draw method which enables us to draw in a destination rectanble (on screen) from source rectangle (in a texture, our sprite sheet !).<\/p>\n<p>Add to that some simple trick in the keyboard input where we would alternate two frames for one specific movement, and you obtain an animated sprite !<\/p>\n<p>Source code : <a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_23.zip\">xna_demo_23<\/a>.<\/p>\n<p>A video (or screenshot) as always.<\/p>\n<div style=\"width: 695px;\" class=\"wp-video\"><video class=\"wp-video-shortcode\" id=\"video-706-1\" width=\"695\" height=\"417\" preload=\"metadata\" controls=\"controls\"><source type=\"video\/mp4\" src=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_23.mp4?_=1\" \/><a href=\"https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_23.mp4\">https:\/\/labalec.fr\/erwan\/wp-content\/uploads\/2013\/11\/xna_demo_23.mp4<\/a><\/video><\/div>\n<p>And the text source code if you want to have a quick glimpse at it.<\/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    Dim graphics As GraphicsDeviceManager<br \/>\n    Dim spriteBatch As SpriteBatch<br \/>\n    Private mTexture As Texture2D<br \/>\n    Dim dest_box As Rectangle<\/p>\n<p>    Dim timer As Integer<br \/>\n    Dim switchFrame As Boolean<br \/>\n    Dim direction_ As Integer<\/p>\n<p>    Public Enum direction<br \/>\n        Up = 0<br \/>\n        Down = 1<br \/>\n        Left = 2<br \/>\n        Right = 3<br \/>\n    End Enum<\/p>\n<p>    Dim frame As Integer = 0<\/p>\n<p>    Public Sub New()<br \/>\n        graphics = New GraphicsDeviceManager(Me)<br \/>\n    End Sub<\/p>\n<p>    Protected Overrides Sub Initialize()<br \/>\n        MyBase.Initialize()<br \/>\n    End Sub<br \/>\n    Protected Overrides Sub LoadContent()<br \/>\n        MyBase.LoadContent()<br \/>\n        spriteBatch = New SpriteBatch(GraphicsDevice)<br \/>\n        'Loading the texture'<br \/>\n        Dim textureStream As System.IO.Stream = New System.IO.StreamReader(Application.StartupPath + \"\\spritesheet.png\").BaseStream<br \/>\n        mTexture = Texture2D.FromStream(GraphicsDevice, textureStream)<br \/>\n        dest_box = New Rectangle(0, 0, 28, 32)<\/p>\n<p>    End Sub<br \/>\n    Protected Overrides Sub UnloadContent()<br \/>\n        MyBase.UnloadContent()<\/p>\n<p>    End Sub<br \/>\n    Protected Overrides Sub Update(ByVal gameTime As Microsoft.Xna.Framework.GameTime)<\/p>\n<p>        Dim newState As KeyboardState<br \/>\n        newState = Keyboard.GetState<\/p>\n<p>        If (newState.IsKeyDown(Keys.Right)) Then<br \/>\n            dest_box.X += 2<br \/>\n            direction_ = direction.Right<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Left)) Then<br \/>\n            dest_box.X -= 2<br \/>\n            direction_ = direction.Left<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Up)) Then<br \/>\n            dest_box.Y -= 2<br \/>\n            direction_ = direction.Up<br \/>\n        End If<\/p>\n<p>        If (newState.IsKeyDown(Keys.Down)) Then<br \/>\n            dest_box.Y += 2<br \/>\n            direction_ = direction.Down<br \/>\n        End If<\/p>\n<p>        animation()<\/p>\n<p>        'if all keys up, then freeze<br \/>\n        If newState.IsKeyUp(Keys.Up) AndAlso newState.IsKeyUp(Keys.Down) AndAlso newState.IsKeyUp(Keys.Left) AndAlso newState.IsKeyUp(Keys.Right) Then<br \/>\n            timer = 0<br \/>\n            switchFrame = False<br \/>\n        End If<\/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)<\/p>\n<p>        spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend)<\/p>\n<p>        spriteBatch.Draw(mTexture, dest_box, New Rectangle(frame * 28, 0, 28, 32), Color.White)<\/p>\n<p>        spriteBatch.End()<br \/>\n        MyBase.Draw(gameTime)<br \/>\n    End Sub<\/p>\n<p>    Sub animation()<\/p>\n<p>        timer += 1<\/p>\n<p>        'every 8 keypress, we switch between frame A and B for one specific move<br \/>\n        If (timer = 8) Then<br \/>\n            switchFrame = True<br \/>\n        ElseIf (timer = 16) Then<br \/>\n            timer = 0<br \/>\n            switchFrame = False<br \/>\n        End If<\/p>\n<p>        If (switchFrame = False) Then<br \/>\n            Select Case direction_<br \/>\n                Case direction.Up<br \/>\n                    frame = 5<br \/>\n                Case direction.Down<br \/>\n                    frame = 2<br \/>\n                Case direction.Left<br \/>\n                    frame = 0<br \/>\n                Case direction.Right<br \/>\n                    frame = 7<br \/>\n            End Select<br \/>\n        Else<br \/>\n            Select Case direction_<br \/>\n                Case direction.Up<br \/>\n                    frame = 4<br \/>\n                Case direction.Down<br \/>\n                    frame = 3<br \/>\n                Case direction.Left<br \/>\n                    frame = 1<br \/>\n                Case direction.Right<br \/>\n                    frame = 6<br \/>\n            End Select<br \/>\n        End If<br \/>\n    End Sub<br \/>\nEnd Class<\/p>\n<p><\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>One thing that gives makes a game more dynamic is animated sprite. Indeed, moving a fix shape all over the screen is nice. Moving an animated shape is better \ud83d\ude42 Lets start with the below sprite sheet (yes it is link &#8211; hope no one will sue me for copyright infringement, this is only for <a href='https:\/\/labalec.fr\/erwan\/?p=706' 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-706","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\/706","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=706"}],"version-history":[{"count":3,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/706\/revisions"}],"predecessor-version":[{"id":712,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=\/wp\/v2\/posts\/706\/revisions\/712"}],"wp:attachment":[{"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=706"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=706"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/labalec.fr\/erwan\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=706"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}