What is the best way to limit Cocoso2d-x FPS?

What is the best way to limit Cocoso2d-x FPS?
0.0 0


I’m looking for a way to ensure the delta time won’t be longer than a specific threshold.
This is important because I notice that while receiving an email while my game is running the FPS drops, and the dt
in all of the update methods is very large, causing my player to die.

How can I limit it in a way that all of the update methods will get my capped dt?



But I think the key is to optimize your game logic. You should find out hotspot firstly.


Have you tried fixed time step?




Thanks for the suggestions. I think I didn’t explain my problem well.
I need to ensure all update methods aren’t being called with a dt time longer than some threshold.
In order to do that, I’ll have to add the following code to every update method like this:

void update(float dt)
if (dt > 1.0f)
// dt can be long when receiving email, I prefer my game to slow down than to skip frames
dt = 1.0f;

// … Rest of my code

Is there a way to do in the one central place?


I don’t think there is a way to do exactly that. Common approach to your problem is conceptually
different - if some activity takes too much time to fit between updates, it should be done in its
own thread. Or you can pause the game until it finished.


Hi Oren,

As I told you Fixed step is the way your updates always will receive the same dt
If you have a big delta between some frames you will update several times your objects
but you set a limitation about how many updates you will do!

Here I explain how to implent it in Cocos2d-x!

I hope it helps.



I think you know the solution by now, but as I found my own problem and found the same place to solve your problem. This is it.

Look in to CCDirector.cpp file, then in calculateDeltaTime() method (base on v.2 but I think it’d be similar for v.3). Look for

#ifdef DEBUG
    // If we are debugging our code, prevent big delta time
    if(m_fDeltaTime > 0.2f)
        m_fDeltaTime = 1/ 60.0f;

I think you can change the threshold there.