Sprites speed

Started by
8 comments, last by arcangelg 22 years, 5 months ago
Hi everybody I am working on an arcade game Now I am creating the sprites and I have a couple of doubts: One of the sprites is a man. This man must walk, run, fight, etc I made 10 frames for walk animation and 10 frames for run animation I dont know how to manage the time for this. Be cause I need that one walk cicle must take one second and the run cicle take 1/2 second (both with 10 frames for each) How can I manage the sprites speed? Please, if you have an example, much better Cheers, Arcangel
Arcangel
Advertisement
You could keep a variable for framerate inside of your sprite. Then, when you switch animation modes, you could switch frame rates. This would allow each animation cycle to run at its own framerate....Just a suggestion.
You need to keep a timer and track how long has passed. I give an example below for a sprite that has 10 frames per second. each Frame occurs every 100ms. The game figures out how much time has passed, and tells the sprite. When the sprite draws itself, it looks at what time it is and decides which frame to draw. The sprite''s time is always % 1000ms, or one second, so figuring out what frame to draw is just determining what percentage of a second has passed, which is obtained by dividing the time by 100.

  //In your idle loopstatic DWORD oldTime;DWORD currentTime;DWORD elapsedTime;//Update the timecurrentTime = timeGetTime ();elapsedTime = currentTime - oldTime;oldTime = currentTime;//Tell the objects what time its supposed to besprite.time += elapsedTime;//////////////////// On Sprite Draw//////////////////sprite.time %= 1000;  //Clamps total animation time to 1000ms or 1 secondsprite.drawFrame (sprite.time / 100);  //Draw frame 0 to 9, based on current time  
Actually, I think you should only update oldTime when a certain interval has passed. I could be wrong.
quote:
Actually, I think you should only update oldTime when a certain interval has passed. I could be wrong


No, not as I wrote it.

You are thinking of something like this:

  static DWORD oldTime;DWORD currentTime;DWORD elapsedTime;currentTime = timeGetTime ();elapsedTime = currentTime - oldTime;if (elapsedTime > 100){   oldTime = currentTime;   frame++;   if (frame > 9)     frame = 0;}drawSprite (frame);  


This method waits until a certain amount of time has passed (.1 seconds) then advances the frame and resets the counter. Its fine if you just need to time one thing. i suggested the other method because I think its easier to have each object manage its own time.
I have done it the same way as invective, calculating the elapsed time as a percentage of a second - then drawing frames or updating positions based on the percentage of their frames/sec or pix/sec speed.

It works a treat allowing faster machines to still achieve a higher frame rate without choking them. And because there are no sleeps or delays the program can continue with other processing.
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Thanks a lot, for your reply

I test it, and it works fine!

Can I use that for the sprites movement? (the amount of pixels for second to achieve the sprite speed)

Cheers, Arcangel
Arcangel
Yeah, for sure - velocity and acceleration. Anything that you would do as a function of time because you are just proportionally increasing the velocity, position by an amount relative to how much time has passed.

I also forgot to mention that I store the values as floats to avoid rounding errors. This is probably fairly obvious, but just to be complete.

Glad to hear it''s all working anyway!
"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
Yeah, it is not a common thing!

Thanks JY, thanks Invective, thanks to everyone

Arcangel
Arcangel
Yeah, it is not a common thing!

Thanks JY, thanks Invective, thanks to everyone

Arcangel
Arcangel

This topic is closed to new replies.

Advertisement