How to slow down a sprite that updates every frame?
Posted
by
xiaohouzi79
on Game Development
See other posts from Game Development
or by xiaohouzi79
Published on 2012-11-21T05:03:38Z
Indexed on
2012/11/21
5:22 UTC
Read the original article
Hit count: 410
I am going through a Allegro 5 tutorial which has a game loop.
There is also a variable "active" which determines if a key is being held down. Thus if the left key is being held down active is on and it begins looping through the row on the sprite sheet that corresponds to moving left.
The problem is that this logic is checked everytime the loop is performed thus at approximately 60 fps the three images that are used to do the left walking animation cycle round super fast which means my character looks like it is in a rush.
Total beginner question: so what is the correct way to slow down the transition between sprites so that the walking looks like it is done at a moderate pace.
Here is the code used to transition across the sprite between the three different phases of the person walking:
if (active) {
sourceX += al_get_bitmap_width(player) / 3;
} else {
sourceX = 32;
}
if (sourceX >= al_get_bitmap_width(player)) {
sourceX = 0;
}
I can kind of guess what it should be in plain English: update sourceX only every certain part of a second but I can't think of how to put this into code.
© Game Development or respective owner