How to limit fps without set forceSetTimeOut=True in Phaser3

Viewed 23

If i wanna limit fps to 30. i can only set forceSetTimeOut=True。But it’s not good as using requestAnimationFrame and count elapsed.

Is there anyway in phaser 3 to perform like the code below?

let fps = 30
let fpsInterval = 1000 / fps
let fpsLast

let transport = undefined

function setFps() {
    fpsLast = new Date().getTime()
}

function setScreen() {
    canvas.width = ctxWidth;
    canvas.height = ctxHeight;
}

function render() {
    frameId = window.requestAnimationFrame(render)
    let now = new Date().getTime()
    let elapsed = now - fpsLast;
    if (elapsed > fpsInterval) {
        fpsLast = now - (elapsed % fpsInterval);
        if(gameRunning){
            main()
        }
    }
}
1 Answers

It is probably not the best solution, but you could use the parameters time from the update function(link to the documentation). Beware: The update function will be called about 60 times per second, but your code inside the if clause won't.
As in the example, that you posted

update (time, delta){
    // 1000ms divided by 31, just to be on the save side
    const timeNeededForAbout30Frame = 1000/31;
    if ((time - this.lastRun) > timeNeededForAbout30Frame ){
        this.lastRun = time;
        // ... here comes the code 
    }
}

Info: I would rather use the fpsConfig (link to the documentation), but since with this config the target fps, are not enforced, just a "hint" it might not work for you.

Related