0
votes

I'm trying to animate a character and make him walk left and right. While I've learned how to make a basic animation, I can't figure out how to switch between them.

When the character walks left I'd like him to switch from the 'idle' (animatedSprite) animation to the 'walking left'(animatedSprite2) animation.

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;

namespace WalkingAnimation
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private AnimatedSprite animatedSprite, animatedSprite2;
        private Vector2 position = new Vector2(350f, 200f);
        private KeyboardState keyState;

        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>("Idle");
            Texture2D texture2 = Content.Load<Texture2D>("Run");
            animatedSprite = new AnimatedSprite(texture, 1, 11);
            animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            keyState = Keyboard.GetState();

            if (keyState.IsKeyDown(Keys.Q))
            {
                position.X -= 3;
            }
            if (keyState.IsKeyDown(Keys.P))
            {
                position.X += 3;
            }

            base.Update(gameTime);
            animatedSprite.Update();
            animatedSprite2.Update();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
            animatedSprite.Draw(spriteBatch, position);
        }
    }
}
2

2 Answers

0
votes

Like you so clearly expressed in your question, all you need to do is store a Boolean which describes whether the character is idle or not:

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;

namespace WalkingAnimation
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        private AnimatedSprite animatedSprite, animatedSprite2;
        private Vector2 position = new Vector2(350f, 200f);
        private KeyboardState keyState;
        private bool idle;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
        }

        protected override void Initialize()
        {
            base.Initialize();
            // Start of as idle
            idle = true;
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            // Textures
            Texture2D texture = Content.Load<Texture2D>("Idle");
            Texture2D texture2 = Content.Load<Texture2D>("Run");
            // Animations
            animatedSprite = new AnimatedSprite(texture, 1, 11);
            animatedSprite2 = new AnimatedSprite(texture2, 1, 10);
        }

        protected override void UnloadContent()
        {
        }

        protected override void Update(GameTime gameTime)
        {
            keyState = Keyboard.GetState();
            idle = true; // If the character doesn't move this will stay true

            if (keyState.IsKeyDown(Keys.Q))
            {
                position.X -= 3;
                idle = false;
            }
            if (keyState.IsKeyDown(Keys.P))
            {
                position.X += 3;
                idle = false;
            }

            base.Update(gameTime);
            animatedSprite.Update();
            animatedSprite2.Update();
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);

            base.Draw(gameTime);
            if(idle)
                animatedSprite.Draw(spriteBatch, position);
            else
                animatedSprite2.Draw(spriteBatch, position);
        }
    }
}

And that should do what you want.

Also, if animations get a bit clunky I would recommend using sprite sheets and creating your own Animation class which can store all the player variables. This is extra-useful if you decide to make your game multiplayer.

0
votes

Lets say left and right walking anmation uses 5 frame. And it's on single texture.

Function Update

    if State = Left 
        currentFrame +=1;
        if currentFrame > 5 then currentFrame = 0
        texureSource = new rectangle(32*currentFrame,0,32,23);
    end if
    if State = Right
        currentFrame +=1;
        if currentFrame > 5 then currentFrame = 0
        texureSource = new rectangle(32*currentFrame,32,32,23);
    end if

End Function