admin管理员组

文章数量:1328003

In my Phaser3 game there is a global gameTick variable that is incremented every update. I am using this to spawn in enemies in my game every 100th update.

Here is a simplifed example of what is going on in my scene class:

update () {
    global.gameTick++;

    if (global.gameTick % 100 === 0) {
        this.spawnAlien();
    }
}

This works fine but as soon as a user plays the game on a monitor with a refresh rate >60hz the update timing breaks and causes the aliens to spawn more frequently.

I have checked this.physics.world.fps and it is 60. I can also modify this.physics.world.timescale but then I would have to do a giant switch statement for every refresh rate.

Either I am missing an obvious solution or my global.gameTick method is not an effective way to acplish this task.

This is what I have in my config so far

let config = {
    type: Phaser.AUTO,
    backgroundColor: "#000",
    scale: {
        parent: "game",
        mode: Phaser.Scale.FIT,
        width: 1900,
        height: 600,
    },
    physics: {
        default: "arcade",
        arcade: {
            debug: true,
            fps: 60 // doesn't fix update frequency
        },
        fps: { // not sure if this is even doing anything
            max: 60,
            min: 20,
            target: 60,
        }
    },
    pixelArt: true,
};

In my Phaser3 game there is a global gameTick variable that is incremented every update. I am using this to spawn in enemies in my game every 100th update.

Here is a simplifed example of what is going on in my scene class:

update () {
    global.gameTick++;

    if (global.gameTick % 100 === 0) {
        this.spawnAlien();
    }
}

This works fine but as soon as a user plays the game on a monitor with a refresh rate >60hz the update timing breaks and causes the aliens to spawn more frequently.

I have checked this.physics.world.fps and it is 60. I can also modify this.physics.world.timescale but then I would have to do a giant switch statement for every refresh rate.

Either I am missing an obvious solution or my global.gameTick method is not an effective way to acplish this task.

This is what I have in my config so far

let config = {
    type: Phaser.AUTO,
    backgroundColor: "#000",
    scale: {
        parent: "game",
        mode: Phaser.Scale.FIT,
        width: 1900,
        height: 600,
    },
    physics: {
        default: "arcade",
        arcade: {
            debug: true,
            fps: 60 // doesn't fix update frequency
        },
        fps: { // not sure if this is even doing anything
            max: 60,
            min: 20,
            target: 60,
        }
    },
    pixelArt: true,
};
Share Improve this question edited Jan 5, 2020 at 18:05 exciteabletom asked Jan 5, 2020 at 15:13 exciteabletomexciteabletom 4717 silver badges15 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5

You can also set the following property in your game's config object:

fps: {
  target: 24,
  forceSetTimeOut: true
},

Source: https://phaser.discourse.group/t/how-to-limit-fps-with-phaser-3/275/14?u=saricden

To limit the update rate, use the following method.

// The time and delta variables are passed to `update()` by Phaser
update(time, delta) {
    this.frameTime += delta

    if (this.frameTime > 16.5) {  
        this.frameTime = 0;
        g.gameTick++;
        // Code that relies on a consistent 60hz update
    }
}

This accumulates the miiliseconds between the last frame and the current frame. It only runs the update() code if there has been 16.5ms of delay.

The example above works for 60fps, but if you want to limit the FPS to a different value use the formula: delay = 1000/fps.

本文标签: javascriptWays to limit Phaser3 update rateStack Overflow