Public Group

How to make an animation timer

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

Recommended Posts

I have a timer but it stops all excution tell it is done. Is there a way to make a timer that will just run and any time it hits the time I need my program will do what ever at that time? if(Tileset.tiles == 15 && Tileset.tiles <=20 && timer == 1/2 sec){ Tileset.tiles = Tileset.tiles++;} so after a 1/2 sec elapsed it will go to the next tile

Share on other sites
does any one know how to make a simple timer?

Share on other sites
Well, you will have to tie it to the game timer some how.
If you are using frame based movment, this kind of thing won't be this hard.

Frame based movment is where you halt all game action is taken until a specific interval has been reached. Say, your game is only updated every 1 1/10th of a second. You would simply increase your player's position after the interval has been met.

The other option is Time based movment. While it is more complicated because you have to interpolate or truncate, it allows for smoother movment.

+------+------+------+------+------+    Say these are the cells in your animation| 1    |  2   | 3    | 4    |  5   ||      |      |      |      |      |+------+------+------+------+------+^111111^222222^333333^444444^555555^    <- *11------2------3------4------5------6    <- *2

with #1, lets say that is the time line syncronised with the elapsed time using time based movment. It has to look up what frame to render every time the function is called. It will look a lot smoother, but it is a more complicated routine

with #2, that represents frame based movment. The counter is automaticaly incremented every frame.

Lets look at time versus frame animation for motion.
With frame based: newx = oldx + increment
With time based: newx = oldx + (velocity * time)

Are you starting to get the picture now?

Share on other sites
From my post here it tells how to use that simple class. So if you want to do what you have said:
// Holds the time counterstatic float mTime = 0;// Holds the last timestatic float mLastTime = timer.GetTime();// Update the timemTime += ( timer.GetTime() - mLastTime );// 500 ms = 1/2 sec, don't use == becuase values are not exactif( Tileset.tiles >= 15 && Tileset.tiles <= 20 && mTime >= 500 ){   /Your code here   Tileset.tiles++;   // reset the timer   mTime = 0;}// Set the new last timemLastTime = timer.GetTime();

Now for that time based movement Mr. Bios has mentioned, use the timer.Get_dt(); function for that. You might want to look at the results it gives and clamp them to a range to filter out any wild peaks.

You should just make one global timer class and use that for all your animations rather than have individual timers. By using the variavles such as mTime and mLastTime you should be able to get what you need on a per animation basics, just don't use static variables, put them in your class instead.

Share on other sites
Only one issue with this. I dont know how to use classes yet. So could you show me a timer that does not use a class?

Share on other sites
Quote:
 Original post by kingpinzsOnly one issue with this. I dont know how to use classes yet. So could you show me a timer that does not use a class?

sure. this is a function to get the elapsed time since the function was last called. This is assuming you are using SDL.
float GetElapsedTime(void){     static int lastTime = SDL_GetTicks();     int newTime = SDL_GetTicks();     float rVal = (float)(newTime - lastTime);     rVal = rVal / 1000.0f;     //turn it into seconds instead of miliseconds     lastTime = newTime;     return rVal;}

I hope you at least understand the static identifier, and the casting with float.
Call this once at the top of every frame to get your change in time.

• What is your GameDev Story?

In 2019 we are celebrating 20 years of GameDev.net! Share your GameDev Story with us.

(You must login to your GameDev.net account.)

• 14
• 10
• 9
• 35
• 16
• Forum Statistics

• Total Topics
634125
• Total Posts
3015674
×