Texture Atlases - Part 3

Sections > 1 - 2 - 3

Using Our AnimatedSprite Class

We will now go back to our main game class and draw an animated sprite with our new AnimatedSprite class that we created in Part 2. Go back over to your main game class (called Game1.cs by default). We will need to make a few simple changes here to get our animated sprite to draw.

First, we will need an instance variable to store our animated sprite, so add the following code as an instance variable in your game class:

private AnimatedSprite animatedSprite;

Next, go down to your LoadContent() method, and add the following code to get your animated sprite ready to use:

Texture2D texture = Content.Load<Texture2D>("SmileyWalk");
animatedSprite = new AnimatedSprite(texture, 4, 4);

These two lines load the texture and then create the animated sprite from it. This texture has four rows and four columns, so we pass those values in as well.

Now all we need to do is update the animated sprite and draw it. To update it, add the following line of code to your Update() method:

animatedSprite.Update();

To draw the animated sprite, add the following line to your Draw() method:

animatedSprite.Draw(spriteBatch, new Vector2(400, 200));

You should now be able to run the game and see the character walking in place!

screenshot1.png

Below is the code for the main game class:

using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
 
namespace TextureAtlas
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private AnimatedSprite animatedSprite;
 
        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }
 
        protected override void Initialize()
        {
            base.Initialize();
        }
 
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
 
            Texture2D texture = Content.Load<Texture2D>("SmileyWalk");
            animatedSprite = new AnimatedSprite(texture, 4, 4);
        }
 
        protected override void UnloadContent()
        {
        }
 
        protected override void Update(GameTime gameTime)
        {
            // Allows the game to exit
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
 
            animatedSprite.Update();
 
            base.Update(gameTime);
        }
 
        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
 
            animatedSprite.Draw(spriteBatch, new Vector2(400, 200));
 
            base.Draw(gameTime);
        }
    }
}

What's Next?

This tutorial covered texture atlases and covered some more features of 2D games. As a next step, you might want to check out the tutorial on rotating sprites to see some more interesting things you can do in a 2D game.

Sections > 1 - 2 - 3


Troubleshooting.png Having problems with this tutorial? Try the troubleshooting page!