Jump to content
  • Advertisement
Sign in to follow this  
JWindebank

Sprite Movement over Time

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

Me again. :) OK, I have figured out how to actually get my Enemy sprites to follow a path, but I am trying to make them actually "move" from one spot to another. I'm trying to work out how to create the following: Enemy move from position A to position B in x seconds. Seconds can be determined as I'm using limiting the frames. So in my Movement function, how would I make the sprite move gradually please? Do I keep track of the frame number by (frame++) after each frame and reset it continually at my "frames per second" amount to get a 'second' loop as such? Eg:
Gameloop:

function()
function()
function()

frame++

if(frame==FRAMES_PER_SECOND)
{
    frames = 0;
}
With that, I can say 'Move from x (100) to x (150) in 2 seconds. So for each frame move 50(pixels) / 2 * frames_per_second? Is that the easiest way to do it?

Share this post


Link to post
Share on other sites
Advertisement
You don't use the fames for that. That's a very bad hack. Instead, use a timer, which updates the tile position depending on their speed and direction.

Share this post


Link to post
Share on other sites
Thanks for the reply Raduprv, do you know where I could possibly read up on the 'timer' you speak of? This is the first I've heard of it?

Also can someone please explain exactly why using frames for movement is a hack?

Thank you. :)

Share this post


Link to post
Share on other sites
Frame based movement is bad because the frame rate is not constant. Some computers are faster, some are slower. Sometimes all the CPU belongs to your game, sometimes only 5% of the CPU can be used by your game. And so on.
As for a timer, it depends what library you are using.
If you are using Win32, just do a search for timer functions. AFAIR, you can set a function to be called when the timer is activated.

Share this post


Link to post
Share on other sites
Thank you for the pointers.

However I am actually limiting my frames to 30fps. Does this make it ok to use frames for timing?

Share this post


Link to post
Share on other sites
Quote:
Original post by JWindebank
Thank you for the pointers.

However I am actually limiting my frames to 30fps. Does this make it ok to use frames for timing?


No, because you can't make sure that the frame rate will always be 30. Sometimes it can be lower than that, especially whe the CPU is busy with other, higher priority things.

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!