Sign in to follow this  

simple animation loop

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this