Jump to content
  • Advertisement
Sign in to follow this  
zoner7

Animations

This topic is 3039 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!