1

Im programming an iOS game and I use the method update for a lot of things, which is called at the game speed refresh (for the moment 60 times per second) but the problem is if the frame rate drops down (for example a notification, or any behavior in the game that, when called, it makes drop down a little bit the fps...) then the bugs comes....

A fast example is if I have an animation of 80 pictures, 40 for jump up and 40 for fall, I would need 1,2 second to run the animation, so if the jump takes 1,2 second it would be ok, the animation would run. But if my fps drop down to 30 then the animation would cut because it would need 2,4 seconds to run the animation but the jump remains 1,2 second. This is only a fast example, there is a lot of unexpected behaviors in the game if the frame rate drops, so my question is, are games developers depending so much on frame rate or there is a way to avoid those fps-bugs? (another way to program or any trick?)

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Alfro
  • 1,524
  • 2
  • 21
  • 37
  • It's extremely hard to help you without more of an idea of all that is going on. You say a notification makes the fps drop. What does that notification trigger ? I think the answers have good suggestions for dealing with a drop. But personally, I'd be investigating the reason for the drop. What is node and draw count and fps on the device ? Is it batch rendering your textures ? – prototypical May 06 '14 at 19:02
  • I added some cool references to FPS vs Time-based techniques on my answer. Check it out! – karlphillip May 22 '14 at 03:04

3 Answers3

6

Base your timing on the time, rather than the frame count. So, save a time stamp on each frame, and on the next frame, calculate how much time has elapsed, and based on your ideal frame rate, figure out how many frames of animation to advance. At full speed, you shouldn’t notice a difference, and when the frame rate drops, your animations may get jerky but the motion will never get more than 1 frame behind where it should be.

Edit: as uliwitness points out, be careful what time function you use, so you don’t encounter issues when, for example, the computer goes to sleep or the game pauses.

Zev Eisenberg
  • 8,080
  • 5
  • 38
  • 82
  • 3
    So pretty much how every game loop, ever, has worked. – trojanfoe May 06 '14 at 14:58
  • 1
    @trojanfoe What's your problem with simple questions? This isn't the first time, you're doing this. If you have an article or duplicate explaining the matter to OP, then link it. Instead of ridiculing people actually trying to help. – Jawap May 06 '14 at 15:06
  • @Jawap Who's ridiculing? This answer is explaining what has always happened with game loops and is not providing "some new insight" into how to solve the specific question asked. A reference or two is in order to provide a more thorough explanation of the process. – trojanfoe May 06 '14 at 15:09
  • I’m not sure how much more detail I could provide without a specific code sample from OP. I could write something generic, but not sure how much it would help. (But I’m trying to be a good SO citizen, so I’m happy to take criticism.) – Zev Eisenberg May 06 '14 at 15:10
  • One caveat when using "the time": Pick the right time function. Usually you want one that counts time the computer is running. The other timing function counts wall clock time. So, if you put your laptop to sleep, the former will pause and continue right where you started, while the latter will immediately jump to the end of the animation. – uliwitness May 06 '14 at 15:10
  • @trojanfoe The OP asked a very basic question, as such I think a very basic answer is the right thing. Don't complain about other peoples' answers. Provide a better one, provide constructive criticism, or remain quiet and let those actually helping get on with it. – uliwitness May 06 '14 at 15:12
  • You should flag the comment for moderator attention if you feel it inappropriate and in the mean time I will comment on what I like, when I like. – trojanfoe May 06 '14 at 15:14
1

Always use the delta value in your update method. This is platform and engine independent. Multiply any speed or change value by the delta value (the time interval between the current and the last frames).

In case of the animation, one way to fix the issue could be to multiply the animation counter by delta (and an inverse of the expected interval). Then round this value to get the correct image for the animation.

// currentFrame is a float ivar set to 0 at the beginning of the animation.
currentFrame = currentFrame + delta * 60.0;
int imageIndex = roundf(currentFrame);

However, with Sprite Kit there is a better way to do this kind of animation, as there is a premade SKAction dealing with sprite animation.

[SKAction animateWithTextures:theTextures timePerFrame:someInterval];

With this solution you don't have to deal with timing the images at all. The engine will do that for you.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
0

There's a great discussion about FPS-based and Time-based techniques here:

It's the best on my opinion, very complete, easy to follow and provides JsFiddle examples. I translated those examples to C++/Qt.

Click here to watch a video of my app running:

karlphillip
  • 92,053
  • 36
  • 243
  • 426