Animations

Started by
1 comment, last by zoner7 13 years, 8 months ago
While working on my 2-d game (written in c++ using SFML), I realized that I have never had to animate sprites before. I want to simulate a figure walking. I know that an animation is essentially a sequence of sprites played in quick succession. My question regards how exactly an animation is programed.

Should there be some sort of switch statement or a sequence of else ifs where I say...

if (currently displaying sprite 1)
display sprite 2
else if (currently displaying sprite 2)
display sprite 3

and so on. Or is there a more conventional approach to my problem here? I imagine I should start by creating an animated class, which has a std::vector of sf::sprites.
Advertisement
You would probably have a sprite class, containing a vector of images, a float timestamp which is incremented and then used with int-cast to index into this vector.

Alternatively, you could put all your frames in one big texture, and then have a vector of texture-uv coordinates you refer to with your index.

Then you could have some kind of "advanceTime (float _deltaTimeInSeconds)" which you call from your logic-loop with the elapsed time since last frame, and increment your sprite-timestamp.

That would give you a basic, animated sprite. Now for a 2d-game, you'll probably want to add different walking directions, which implies more vectors with different animations (or a bigger texture containing all frames) and some kind of statemachine determining which animation to take, depending on the movement direction.

Regards
visit my website at www.kalmiya.com
So could I basically have a large sprite sheet, perhaps with four columns, each with a sequence of sprites representing the animation that would be displayed when a character walks in one of the four different directions?

Then my animation class would know that when dt passes, it only needs to look x numbers of pixels down the column of the sprite sheet for the next sprite to display, assuming the direction that the character moves remains constant.

This topic is closed to new replies.

Advertisement