• (2 Pages)
  • +
  • 1
  • 2

XNA Animated Sprite

#1 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 13 October 2010 - 06:38 PM

*
POPULAR

Animated Sprite Tutorial:

This is actually the first tutorial I’ve ever written, so I hope I make this as clear as possible. This is going to be a simple tutorial on the basics of looping through the “frames” of a spritesheet to create the illusion of a walking character. Generally when I think of animations pulled from spritesheets, I think of the old school 8bit and 16bit RPG’s that were the glory days of gaming, so I’m going to use a simple 16 “frame” walk cycle for a character that I took about five minutes of my time to create using some online sprite sheet generator that I found several months ago. I can’t remember the site, and unfortunately, I didn’t bookmark it.

I’ll upload the file that I used just to make things simpler than having you find your own file that matches mine close enough to avoid any confusion but I take no credit for the creation of the image and unfortunately, as I mentioned above, I do not remember the site that is responsible for it.

The image I have is a 512x48 pixel PNG file. Each image is 32 pixels wide and 48 pixels tall. There are 4 images walking to the left, 4 images walking to the right, 4 images walking down, and 4 images walking up. With these 16 images, we can create a reasonably realistic walk cycle for any cardinal direction.

Once we have our sprite sheet, we’ll want to start an XNA project and import the image file into the content folder of the project, and I’m assuming you already know how to do this. If not, then check around other places because I’m not going to be covering that particular topic.

Once the file has been added to the project, we’ll start by adding a new class to the project by right clicking the name of the project in the Solution Explorer on the right side of the screen, choose Add->New Item, then choose Class from the window that pops up. We’ll name this class AnimatedSprite.cs. This will be the class that handles the majority of the logic behind creating an animated sprite. Once the class opens up, it’ll be mostly empty. For starters, at the top of the page, add some using statements for the XNA framework namespaces that we will be using in this file. Without these using statements, you’ll run into a lot of errors.
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;



Now that we have that simple step out of the way we can start the fun stuff. First thing we’ll do is create some variables inside the class.
Texture2D spriteTexture;
float timer = 0f;
float interval = 200f;
int currentFrame = 0;
int spriteWidth = 32;
int spriteHeight = 48;
int spriteSpeed = 2;
Rectangle sourceRect;
Vector2 position;
Vector2 origin;



Let’s take a minute to explain these variables so that we’re completely clear on what they are and what they’ll be used for. Texture2D spriteTexture; is pretty self explanatory. It’s an 2d texture object that we are going to use as our sprite. float timer; is a floating point number that will be used for creating a certain amount of time that is required to pass before the sprite will move to the next “frame”. float interval = 200f; is a floating point number that we will use to determine how often we want to step to the next frame in the animation.int currentFrame; is used to keep track of the current frame of the animation. int spriteWidth and int spriteHeight are just used to keep track of the width and height of our sprite as their names suggest. int spriteSpeed; is used to set the speed that the sprite actually moves across the screen. Rectangle sourceRect; is the rectangle in which our sprite will be drawn.Vector2 position; and Vector2 origin; are 2d vectors that will be used to denote where the sprite is on the screen and the center of our drawn sprite.

Alright, now that those variables have been explained, let’s move on to the part where they’re actually being put to use. The following code just sets up the properties for getting and setting the above variables.
public Vector2 Position
{
	get { return position; }
	set { position = value; }
}

public Vector2 Origin
{
	get { return origin; }
	set { origin = value; }
}

public Texture2D Texture
{
	get { return spriteTexture; }
	set { spriteTexture = value; }
}

public Rectangle SourceRect
{
	get { return sourceRect; }
	set { sourceRect = value; }
}



To keep this tutorial from being too long, I’m not going to explain everything about this code. If you have questions about how these work, feel free to ask me, or do a little research and you’ll find plenty of sources that explain properties in depth.

The next bit we need to tackle is the constructor for this class. Following is the code for the constructor.
public AnimatedSprite(Texture2D texture, int currentFrame, int spriteWidth, int spriteHeight)
{
	this.spriteTexture = texture;
	this.currentFrame = currentFrame;
	this.spriteWidth = spriteWidth;
	this.spriteHeight = spriteHeight;
}



The constructor is basically just a method that initializes and creates instances of the data types that we create by creating a class. Each class that you create can be viewed as a new data type. The constructor’s parameters are often used as shortcuts for setting the values of the properties that you defined above the constructor.

In order to allow our sprite to move, we’ll want to check keyboard states to see which keys, if any, are currently pressed. At the top of the class, before the properties, and just before the variables that we declared earlier, we need two variables of type KeyboardState.

KeyboardState currentKBState;
KeyboardState previousKBState;



Now that we have variables created for storing keyboard states, lets utilize them and create a method to handle the movement of our sprite.

public void HandleSpriteMovement(GameTime gameTime)
{
	previousKBState = currentKBState;
	currentKBState = Keyboard.GetState();

	sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);

	if (currentKBState.GetPressedKeys().Length == 0)
	{
		if (currentFrame > 0 && currentFrame < 4)
		{
			currentFrame = 0;
		}
		if (currentFrame > 4 && currentFrame < 8)
		{
			currentFrame = 4;
		}
		if (currentFrame > 8 && currentFrame < 12)
		{
			currentFrame = 8;
		}
		if (currentFrame > 12 && currentFrame < 16)
		{
			currentFrame = 12;
		}
	}

	// This check is a little bit I threw in there to allow the character to sprint.
	if (currentKBState.IsKeyDown(Keys.Space))
	{
		spriteSpeed = 3;
		interval = 100;
	}
	else
	{
		spriteSpeed = 2;
		interval = 200;
	}

	if (currentKBState.IsKeyDown(Keys.Right) == true)
	{
		AnimateRight(gameTime);
		if (position.X < 780)
		{
			position.X += spriteSpeed;
		}
	}

	if (currentKBState.IsKeyDown(Keys.Left) == true)
	{
		AnimateLeft(gameTime);
		if (position.X > 20)
		{
			position.X -= spriteSpeed;
		}
	}

	if (currentKBState.IsKeyDown(Keys.Down) == true)
	{
		AnimateDown(gameTime);
		if (position.Y < 575)
		{
			position.Y += spriteSpeed;
		}
	}

	if (currentKBState.IsKeyDown(Keys.Up) == true)
	{
		AnimateUp(gameTime);
		if (position.Y > 25)
		{
			position.Y -= spriteSpeed;
		}
	}

	origin = new Vector2(sourceRect.Width / 2, sourceRect.Height / 2);
}



The code above might look like a lot, but it isn’t. It first checks to see if any keys are currently pressed. If no keys are pressed, then it checks to see what the current frame of the animation is and sets the frame to the start of that particular walk cycle so that it looks like the character stopped walking and returned to a resting position. Then we check to see which keys are pressed, the sprint section is completely optional and it’s just something I threw in there because I like to have the option to be able to move faster when I feel like it, if up, down, left, or right are pressed then we animate the sprite by calling the appropriate method that we will get to in just a few minutes, and as long as our sprite isn’t hitting the boundaries of the screen, which is where those magic numbers in the ifchecks came from, then we apply the speed to the position of the sprite moving in the appropriate direction.

Alright, it’s time to move to the actual methods that create the animation of the sprite and moves from one frame to another. To accomplish this, we use the following code:
public void AnimateRight(GameTime gameTime)
{
	if (currentKBState != previousKBState)
	{
		currentFrame = 9;
	}

	timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

	if (timer > interval)
	{
		currentFrame++;
		
		if (currentFrame > 11)
		{
			currentFrame = 8;
		}
		timer = 0f;
	}
}



Above is the code for the AnimateRight() method. The other three methods that we will create in a minute are just the same, except they deal with the proper frames inside our sprite sheet. After we change the current frame, the timer is reset and continues to increment. Following is the code for the AnimateLeft(), AnimateUp(), and AnimateDown() methods. I’m not going to go through and explain each one, since you should be able to figure out what each of them do just by reading the explanation of the AnimateRight() method from above.

public void AnimateUp(GameTime gameTime)
        {
            if (currentKBState != previousKBState)
            {
                currentFrame = 13;
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 15)
                {
                    currentFrame = 12;
                }
                timer = 0f;
            }
        }

        public void AnimateDown(GameTime gameTime)
        {
            if (currentKBState != previousKBState)
            {
                currentFrame = 1;
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 3)
                {
                    currentFrame = 0;
                }
                timer = 0f;
            }
        }

        public void AnimateLeft(GameTime gameTime)
        {
            if (currentKBState != previousKBState)
            {
                currentFrame = 5;
            }

            timer += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (timer > interval)
            {
                currentFrame++;

                if (currentFrame > 7)
                {
                    currentFrame = 4;
                }
                timer = 0f;
            }
        }



That is the end of our AnimatedSprite class. Don’t forget to add 2 closing brackets to the end of the file to close out our class to prevent errors.

From here we’ll open the Game1.cs class. At the top of the class, we’ll need to add a sprite batch declaration: SpriteBatch spriteBatch;. After declaring the sprite batch object, we also need to declare a variable of the type we just created. AimatedSprite sprite;. Now move down to the LoadContent() method and create an instance for our spriteBatch; spriteBatch = new SpriteBatch(GraphicsDevice);. We also need to create an instance for our sprite, sprite = new AnimatedSprite(Content.Load<Texture2D>(“Sprite_Sheet”), 1, 32, 48);. We also need to determine the starting position of our sprite, sprite.Position = new Vector2(400, 300);. I chose to start the sprite in the middle of the screen. 400 x 300 is the center of the screen as long as XNA is rendering to a window with the default size of 800 x 600. You can change the values inside the parenthesis if you want your sprite to start elsewhere. Following is the entire code for the LoadContent method just incase I was a bit confusing with the above paragraph:
protected override void LoadContent()
{

	spriteBatch = new SpriteBatch(GraphicsDevice);

	sprite = new AnimatedSprite(Content.Load<Texture2D>(“Sprite_Sheet”), 1, 32, 48);

	sprite.Position = new Vector2(400, 300);
}



The next place we need to add code is the Update method of the Game1 class. This will just be a single line, which can be placed at the top of the method just inside the brackets. sprite.HandleSpriteMovement(gameTime);. This call in the update method will call our HandleSpriteMovement method from the AnimatedSprite class that we wrote earlier. Each time the game is updated, this method will be called and the logic inside it will be performed. If you only want the sprite to animate at certain times, for instance when the game is not paused, then you can throw that call inside an ifcheck inside the update method.

The only real thing left to do is the add a few lines to our draw method to draw the sprite to the screen. Following is the entire code for the draw method:
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.DarkOliveGreen);

            spriteBatch.Begin();
            spriteBatch.Draw(sprite.Texture, sprite.Position, sprite.SourceRect, Color.White, 0f, sprite.Origin, 1.0f, SpriteEffects.None, 0);
            spriteBatch.End();

            base.Draw(gameTime);
}



The code within the draw method is pretty simple, we tell the spritebatch to start and then we tell it to draw our sprite using the texture, position, rectangle, color, rotation, origin, scale, sprite effects, and layer depth. I won’t go into detail about sprite effects or layer depth because that’s beyond what this tutorial is supposed to cover. Finally, we tell the spritebatch to stop.

And that’s it! That’s all it takes to animate a simple spritesheet. If you have any questions, or want me to explain anything a little better, feel free to let me know. I actually enjoyed writing this, so I think I’ll do a few more over the next month or so when I find time. I’m open to suggestions.

Attached image(s)

  • Attached Image

This post has been edited by Kilorn: 11 October 2011 - 03:08 PM


Is This A Good Question/Topic? 8
  • +

Replies To: XNA Animated Sprite

#2 Soyaku  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 47
  • Joined: 28-February 08

Posted 01 November 2010 - 01:55 PM

If the sprite sheet you were utilizing was horizontal and vertical, say 7 rows and 18 columns, how would you change this code to loop through a sprite sheet then?
Was This Post Helpful? 0
  • +
  • -

#3 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 01 November 2010 - 04:56 PM

You would have to change the Y component of sourceRect to begin the rectangle at the right place on the sprite sheet.
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);


Above is the line that would need to be altered with the current code, the 0 in there represents the Y coordinate of the top left corner of the rectangle.

This post has been edited by Kilorn: 01 November 2010 - 04:56 PM

Was This Post Helpful? 0
  • +
  • -

#4 Guest_Elosha*


Reputation:

Posted 09 November 2010 - 08:49 PM

I'm getting an error in the Game1.cs load content area stating:

"animate.AnimatedSprite' does not contain a constructor that takes 1 arguments"

I've tried following your instructions 3 times and come across the same problem on every occasion. I'm using XNA 4.0 if that's any help, but I would have assumed everybody else was too, by now.

Just checked it in XNA 3.1 using VS 2008 - same problem.
Was This Post Helpful? 0

#5 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 10 November 2010 - 06:52 AM

Paste your code here inside code tags like :code:, and let me take a look at it. I think you've probably got a simple syntax error somewhere that's causing this issue.

EDIT: Forgot to mention that this was done with XNA 3.1. I haven't made the full migration to 4.0 yet, but everything here should still work just fine in 4.0.

This post has been edited by Kilorn: 10 November 2010 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#6 ThisIsPathetic  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 16
  • Joined: 10-November 10

Posted 10 November 2010 - 03:31 PM

Awesome tutorial! I learned a lot from this to make my own sprite class... My only question is, I've been looking up ways to add a sprite during runtime, for example, "shoot a fireball" but it seems that the way other people are doing it are adding their own draw methods, and such, to their sprite classes...

anyway you could expand on this tutorial that uses this class that you made to bring onscreen a "new" sprite when "whatever-key" is pressed??
Was This Post Helpful? 0
  • +
  • -

#7 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 10 November 2010 - 06:52 PM

I could possibly throw something together like that if I find the time in the next couple of weeks. I'll see what I can do.
Was This Post Helpful? 0
  • +
  • -

#8 Guest_Elosha*


Reputation:

Posted 14 November 2010 - 02:26 AM

View PostKilorn, on 01 November 2010 - 03:56 PM, said:

You would have to change the Y component of sourceRect to begin the rectangle at the right place on the sprite sheet.
sourceRect = new Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight);


Above is the line that would need to be altered with the current code, the 0 in there represents the Y coordinate of the top left corner of the rectangle.


It was the quotation marks in the loadcontent code I copied from your post, apparently. They were...curlier than usual, and manually replacing them seemed to fix things.

Seriously, not even joking - give it a try (although knowing my luck, it'll be fine).

But hey, thanks for the tutorial man - it's been a huge help to me.
Was This Post Helpful? 0

#9 manafi  Icon User is offline

  • New D.I.C Head

Reputation: 5
  • View blog
  • Posts: 46
  • Joined: 06-November 08

Posted 27 December 2010 - 01:00 PM

I knew that this sprite sheet looked familiar. And, just like you, I had created a character before but couldn't remember the site either. Nevertheless, I just found the site where you can construct your own character:

charas-project.net

Hopefully, this helps someone else out there.
Was This Post Helpful? 1
  • +
  • -

#10 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 27 December 2010 - 01:48 PM

Yea, I've been using that site quite a bit since making this tutorial. I always have to edit the sprite sheets that I get from them because I prefer to layout my walk cycles the way this tutorial shows, so I generally create a character, then spend a little time in Photoshop cutting it up and moving some stuff around. Definitely an awesome site for character sprites, though.
Was This Post Helpful? 0
  • +
  • -

#11 BigBlueCloud  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 09-April 11

Posted 27 April 2011 - 04:49 PM

Awesome tutorial, thanks a lot for posting it. This has really brought me up to speed on sprite animation, and now that I've read through this and got it all working I get to do the fun part... Modding the code to make suit my needs.
Thanks again. :)
Was This Post Helpful? 0
  • +
  • -

#12 markap  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 18-October 11

Posted 18 October 2011 - 10:52 PM

Here is the VB.Net conversion of this class, the notable changes were to how the properties are handled.

Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports Microsoft.Xna.Framework.Input

Public Class AnimatedSprite

    Dim currentKBState As KeyboardState
    Dim previousKBState As KeyboardState

    Dim spriteTexture As Texture2D
    Dim timer As Single = 0.0F
    Dim interval As Single = 200.0F
    Dim currentFrame As Integer = 0
    Dim spriteWidth As Integer = 32
    Dim spriteHeight As Integer = 48
    Dim spriteSpeed As Integer = 2
    
    Dim _sourceRect As Rectangle
    Dim _position As Vector2
    Dim _origin As Vector2

    Public Property Position() As Vector2
        Get
            Return _position
        End Get
        Set(value As Vector2)
            _position = value
        End Set
    End Property

    Public Property Origin() As Vector2
        Get
            Return _origin
        End Get
        Set(value As Vector2)
            _origin = value
        End Set
    End Property

    Public Property Texture() As Texture2D
        Get
            Return spriteTexture
        End Get
        Set(value As Texture2D)
            spriteTexture = value
        End Set
    End Property

    Public Property SourceRect() As Rectangle
        Get
            Return _sourceRect
        End Get
        Set(value As Rectangle)
            _sourceRect = value
        End Set
    End Property

    Public Sub New(texture As Texture2D, currentFrame As Integer, spriteWidth As Integer, spriteHeight As Integer)
        Me.spriteTexture = texture
        Me.currentFrame = currentFrame
        Me.spriteWidth = spriteWidth
        Me.spriteHeight = spriteHeight
    End Sub

    Public Sub HandleSpriteMovement(gameTime As GameTime)
        previousKBState = currentKBState
        currentKBState = Keyboard.GetState()

        sourceRect = New Rectangle(currentFrame * spriteWidth, 0, spriteWidth, spriteHeight)

        If currentKBState.GetPressedKeys().Length = 0 Then
            If currentFrame > 0 AndAlso currentFrame < 4 Then
                currentFrame = 0
            End If
            If currentFrame > 4 AndAlso currentFrame < 8 Then
                currentFrame = 4
            End If
            If currentFrame > 8 AndAlso currentFrame < 12 Then
                currentFrame = 8
            End If
            If currentFrame > 12 AndAlso currentFrame < 16 Then
                currentFrame = 12
            End If
        End If

        ' This check is a little bit I threw in there to allow the character to sprint.
        If currentKBState.IsKeyDown(Keys.Space) Then
            spriteSpeed = 3
            interval = 100
        Else
            spriteSpeed = 2
            interval = 200
        End If

        Dim Pos As Vector2
        Pos = Position
        If currentKBState.IsKeyDown(Keys.Right) = True Then
            AnimateRight(gameTime)
            If Pos.X < 780 Then
                Pos.X += spriteSpeed
            End If
        End If

        If currentKBState.IsKeyDown(Keys.Left) = True Then
            AnimateLeft(gameTime)
            If Pos.X > 20 Then
                Pos.X -= spriteSpeed
            End If
        End If

        If currentKBState.IsKeyDown(Keys.Down) = True Then
            AnimateDown(gameTime)
            If Pos.Y < 575 Then
                Pos.Y += spriteSpeed
            End If
        End If

        If currentKBState.IsKeyDown(Keys.Up) = True Then
            AnimateUp(gameTime)
            If Pos.Y > 25 Then
                Pos.Y -= spriteSpeed
            End If
        End If

        Position = Pos

        Origin = New Vector2(CSng(SourceRect.Width / 2), CSng(SourceRect.Height / 2))
    End Sub

    Public Sub AnimateRight(gameTime As GameTime)
        If currentKBState <> previousKBState Then
            currentFrame = 9
        End If

        timer += CSng(gameTime.ElapsedGameTime.TotalMilliseconds)

        If timer > interval Then
            currentFrame += 1

            If currentFrame > 11 Then
                currentFrame = 8
            End If
            timer = 0.0F
        End If
    End Sub

    Public Sub AnimateUp(gameTime As GameTime)
        If currentKBState <> previousKBState Then
            currentFrame = 13
        End If

        timer += CSng(gameTime.ElapsedGameTime.TotalMilliseconds)

        If timer > interval Then
            currentFrame += 1

            If currentFrame > 15 Then
                currentFrame = 12
            End If
            timer = 0.0F
        End If
    End Sub

    Public Sub AnimateDown(gameTime As GameTime)
        If currentKBState <> previousKBState Then
            currentFrame = 1
        End If

        timer += CSng(gameTime.ElapsedGameTime.TotalMilliseconds)

        If timer > interval Then
            currentFrame += 1

            If currentFrame > 3 Then
                currentFrame = 0
            End If
            timer = 0.0F
        End If
    End Sub

    Public Sub AnimateLeft(gameTime As GameTime)
        If currentKBState <> previousKBState Then
            currentFrame = 5
        End If

        timer += CSng(gameTime.ElapsedGameTime.TotalMilliseconds)

        If timer > interval Then
            currentFrame += 1

            If currentFrame > 7 Then
                currentFrame = 4
            End If
            timer = 0.0F
        End If
    End Sub
End Class




Here is the VB.Net version of Game1.vb to go with the class

''' <summary>
''' This is the main type for your game
''' </summary>
Public Class Game1
    Inherits Microsoft.Xna.Framework.Game

    Private WithEvents graphics As GraphicsDeviceManager
    Private WithEvents spriteBatch As SpriteBatch
    Dim sprite As AnimatedSprite

    Public Sub New()
        graphics = New GraphicsDeviceManager(Me)
        Content.RootDirectory = "Content"
    End Sub

    ''' <summary>
    ''' Allows the game to perform any initialization it needs to before starting to run.
    ''' This is where it can query for any required services and load any non-graphic
    ''' related content.  Calling MyBase.Initialize will enumerate through any components
    ''' and initialize them as well.
    ''' </summary>
    Protected Overrides Sub Initialize()
        ' TODO: Add your initialization logic here
        MyBase.Initialize()
    End Sub

    ''' <summary>
    ''' LoadContent will be called once per game and is the place to load
    ''' all of your content.
    ''' </summary>
    Protected Overrides Sub LoadContent()
        ' Create a new SpriteBatch, which can be used to draw textures.
        spriteBatch = New SpriteBatch(GraphicsDevice)
        sprite = New AnimatedSprite(Content.Load(Of Texture2D)("dudeski"), 1, 32, 48)
        sprite.Position = New Vector2(400, 300)

        ' TODO: use Me.Content to load your game content here
    End Sub

    ''' <summary>
    ''' UnloadContent will be called once per game and is the place to unload
    ''' all content.
    ''' </summary>
    Protected Overrides Sub UnloadContent()
        ' TODO: Unload any non ContentManager content here
    End Sub

    ''' <summary>
    ''' Allows the game to run logic such as updating the world,
    ''' checking for collisions, gathering input, and playing audio.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Update(ByVal gameTime As GameTime)
        ' Allows the game to exit
        If GamePad.GetState(PlayerIndex.One).Buttons.Back = ButtonState.Pressed Then
            Me.Exit()
        End If

        Dim keybState As KeyboardState = Keyboard.GetState()
        If keybState.IsKeyDown(Keys.Escape) Then
            Me.Exit()
        End If

        sprite.HandleSpriteMovement(gameTime)

        ' TODO: Add your update logic here
        MyBase.Update(gameTime)
    End Sub

    ''' <summary>
    ''' This is called when the game should draw itself.
    ''' </summary>
    ''' <param name="gameTime">Provides a snapshot of timing values.</param>
    Protected Overrides Sub Draw(ByVal gameTime As GameTime)
        GraphicsDevice.Clear(Color.CornflowerBlue)
        spriteBatch.Begin()
        spriteBatch.Draw(sprite.Texture, sprite.Position, sprite.SourceRect, Color.White, 0.0F, sprite.Origin, 1.0F, SpriteEffects.None, 0)
        spriteBatch.[End]()
        ' TODO: Add your drawing code here
        MyBase.Draw(gameTime)
    End Sub

End Class



Was This Post Helpful? 1
  • +
  • -

#13 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 19 October 2011 - 05:41 AM

Wow. Good job taking the time to convert it to VB.
Was This Post Helpful? 0
  • +
  • -

#14 HardTek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 16-December 11

Posted 16 December 2011 - 06:04 AM

Hi Kilorn,
thanks for the tutorial...really good job and it works perfectly! ;)
But I have a question,I use that kind of sprites:

Posted Image

How Can I edit the source code for use with that kind of sprites?
Thanks :)
Was This Post Helpful? 0
  • +
  • -

#15 Kilorn  Icon User is offline

  • XNArchitect
  • member icon



Reputation: 1355
  • View blog
  • Posts: 3,528
  • Joined: 03-May 10

Posted 16 December 2011 - 06:45 AM

For a spritesheet layout like that, I wouldn't edit the code in this tutorial, I would instead follow SixOfEleven's sprite animation tutorial as his tutorial works with those 3x4 spritesheet layouts.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2