Jump to content
  • Advertisement
Sign in to follow this  

simple animation loop

This topic is 4163 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

Hello! I'm currently making a game in java (only because I'm more familiar with it than c++) and I'm trying to make a simple sprite animation. I'm sure the concept should be the same no matter what language is used. So, I have three iamges that I want to loop when the up arrow key is pressed, three to loop when down is pressed, same for left and right (making it look like the sprite is walking)...The question is, how would I go about making this loop? Is the while loop best for this sort of thing? I've tried a few different things on my own but so far I haven't been able to make it loop like it should. Any suggestions would be appreciated!

Share this post

Link to post
Share on other sites
Im not sure of how it would be done in Java.

For C++, Normally you would use a timer for each object that needs
to be animated. This indicates how much frames must pass before the
animation update:

class AnimatedSprite {

// timer used in updating animation frames
unsigned int m_iTicks;

// Current frame inside animation
unsigned int m_iCurFrame;
unsigned int m_iMaxFrame;

// current animation (Mabey WALK_LEFT, WALK_RIGHT, etc...)
unsigned int m_iCurAnim;


void Update () {

if (++m_iTicks<FRAME_THRESHOLD) {
// update next frame

if (++m_iCurFrame >= m_iMaxFrame)
m_iCurFrame; // go back to first frame

// render image based on current animation and frame.
// This depends on how your image(s) are loaded. ie,
// are they seperate images per frame or a single templated image?

AnimatedSprite::Update() is called either directly or indirectly
each frame within the game loop.

If the loop is locked to a certain FPS, the above timing code will work
"as is". If it is time-based, you will need to use an actual timing method.

When the player presses left, up, right, or down keys, just change the
sprites current animation to WALK_LEFT, WALK_RIGHT, etc.. and reset
current frame to 0.

This is one possible method you can try...

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!