Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

This question already has an answer here:

At the moment I have a simple static texture. Now I want to animate my sprite. The texture is a simple human. When the user press the enter button, the human gets into state2. This is the standart state during gameplay. If the user presses now "space" the sprite should get into state4(4th texture). This would be very unsmooth, so I decided to make a State3(ist just displayed very short to make the animation smooth). Now the sprite is in state4. If the user now releas the "space"-key the sprite should return into State2. For smoothness there is State3 short between.

State1 "press enter" --> State2 --- "press space" --> (short State3 for smoothness) --> State4 --> "release space" --> (short State3) --> State2

I hope you understand what I mean :)

michi

share|improve this question

marked as duplicate by Byte56, Josh Petrie, Sean Middleditch, Tetrad Feb 12 '13 at 17:28

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I'd recommend having a look at the official MSDN tutorial that covers animations, among other things: xbox.create.msdn.com/en-US/education/tutorial/2dgame/… –  Hackworth Feb 12 '13 at 10:55
    
There you see just like on all the other sites who you can make a always repeating linear animation. –  user26188 Feb 12 '13 at 11:18
    
I recommend you look through the code and try to understand it. In the animation class, there is a "bool Looping". Unsurprisingly, it determines whether the animation will loop or not. Simply always set it to false and leave it there to get exactly one run. Add another "bool Reverse" that causes to play the animation in reverse. When the user presses space, set active to true. When the user releases the space bar, set reverse to true, play the animation in reverse, then set active to false when the animation is done. –  Hackworth Feb 12 '13 at 12:32
    
Ok. I understand what you mean. What is your question? –  Anko Feb 12 '13 at 13:26
    
Please edit your old question with the additional details, don't start a new question. –  Byte56 Feb 12 '13 at 14:58

1 Answer 1

Here is a simple class that you can reuse if you do it right, you can for example set a counter that counts down whenever the player is moving and create an animation from a "Sprite Strip"

public class SpriteAnimation : Microsoft.Xna.Framework.DrawableGameComponent
{
    int Rows, CurrentRow, Columns, CurrentColumn, X, Y, Width, Height;
    Texture2D Texture;
    SpriteBatch SpriteBatch;
    Rectangle destRectangle, sourceRectangle;

    public SpriteAnimation(int rows, int columns, Texture2D texture, SpriteBatch spriteBatch, Game game)  : base(game)
    {
        this.Rows = rows;
        this.Columns = columns;
        this.SpriteBatch = spriteBatch;
        this.Texture = texture;
        this.Width = this.Texture.Width / this.Columns;
        this.Height = this.Texture.Height / this.Rows;

        SetPosition(0, 0);
        SetFrame(0, 0);
    }

    public void SetPosition(int x, int y)
    {
        X = x;
        Y = y;

        destRectangle = new Rectangle(X, Y, Width, Height);
    }

    public void SetFrame(int row, int column)
    {
        if (row >= 0 && row <= (Rows - 1))
        {
            CurrentRow = row;
        }

        if (column >= 0 && column <= (Columns - 1))
        {
            CurrentColumn = column;
        }

        sourceRectangle = new Rectangle((CurrentColumn * Width), (CurrentRow * Height), Width, Height);
    }

    public override void Update(GameTime gameTime)
    {
        base.Update(gameTime);

        KeyboardState state = Keyboard.GetState();

        if (state.IsKeyDown(Keys.Space)) { /* SetFrame(0, 1) */ }
    }

    public override void Draw(GameTime gameTime)
    {
        base.Draw(gameTime);

        SpriteBatch.Begin();
        SpriteBatch.Draw(Texture, destRectangle, sourceRectangle, Color.White);
        SpriteBatch.End();

    }
}

If you check the link above that I include "Sprite Strip", you see that they're divided into columns and rows and that's what you use them for. For your animation, create a texture that has all four states and then you can jump between them whenever you need. So if you have all the different state textures on one row, then row = 1, columns = 4. Then you can extend this class to make it handle an animation yourself.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.