How to deal with framerate drop?

Hello all.

I’m new to gamedev, but I’m a javascript developer for many years. So I’m trying my luck with Phaser while making some games, but many times I question myself if what I’m doing is the correct way of doing it or not.

For example, in my game I have some parts of the stage that are really heavy, full of game objects and other things and in these parts, I get heavy framerate drops.

I also have a feature where if you hold the jump button for longer, the hero will jump higher. So in the parts where the framedrop happens, if I hold the jump button, the hero will jump higher than where there;s no framedrop, because the update function is called less often, so the code that stops the hero velocity is only run later.

How can I fix this? I noticed that the update function contains a time and delta variables, but since I don’t know anything about gamedev, I don’t know how to use them or if they will solve my problem.

Thank you

You should never assume that update will run at a consistent frequency. Aside from running less often if the game is lagging, it will run more often if the player is using a monitor with a higher refresh rate, such as 144 Hz.

The parameters you mentioned are the solution to this problem. delta contains the number of milliseconds that have elapsed since the last time update was called. If you want to do anything that depends on the time elapsed, you should accumulate the delta values to figure out how much time has passed. For example, you could set a variable to 3000 and decrease it by delta in the update method; when the variable reaches 0 (or becomes negative), 3 seconds have elapsed.

The time parameter is the current time (probably relative to when the page was opened, but I’m not sure), again in milliseconds. Personally, I recommend against using it. Unlike the delta, which is smoothed by Phaser, time can sporadically change because it keeps ticking if the game or the scene pause.

You can also take a look at Phaser’s clock and timers, which can automate some of this logic for you.

1 Like

This is precisely what I wanted to know. Thank you. I’ll work on all these changes in the weekend. Exciting.