# Is there a way to program games without depending on frame rate?

I'm programming an iOS game and I'm using method update for a lot of things that gets called when the game speed is updated (60 times per second at the moment), but the problem is that the frame rate drops (like a notification or any behavior in the game that decreases fps slightly when called ...) then errors appear ...

A quick example: if I have an animation of 80 images, 40 for a jump and 40 for a fall, it will take me 1.2 seconds for the animation to start, so if the jump takes 1.2 seconds, that is fine, the animation will work. But if my fps drops to 30, the animation will shrink because it takes 2.4 seconds to start the animation, but the jump remains 1.2 seconds. This is just a quick example: the game has a lot of unexpected behavior if the frame rate drops, so my question is if game developers rely so heavily on frame rate or is there a way to avoid these fps errors? (another programming way or any trick?)

+1

source to share

Adjust the time to the time, not the number of frames. So, save the time stamp on each frame, and on the next frame, calculate how much time has passed, and based on your ideal frame rate, figure out how many frames of the animation to advance. At full speed, you won't notice the difference, and when the frame rate drops, your animations can get choppy, but the motion will never get more than 1 frame where it should be.

Edit: As a matter of obviousness, be careful which timing function you use, so you don't run into problems when, for example, the computer goes to sleep or the game pauses.

+6

source

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

In the case of animation, one way to fix the problem might be to multiply the animation counter by the delta (and invert the expected interval). Then round this value to get the correct image for your 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 the Sprite Kit, there is a better way to make this kind of animation, as there is a ready-made SKAction associated with sprite animation.

``````[SKAction animateWithTextures:theTextures timePerFrame:someInterval];
```

```

With this solution, you don't have to deal with image sync. The engine will do it for you.

+1

source

There is a great discussion here about FPS and Time based methods:

This is the best in my opinion, very complete, easy to follow and provides JsFiddle examples. I have translated these examples to C ++ / Qt .