简体   繁体   English

限制 Phaser3 更新率的方法?

[英]Ways to limit Phaser3 update rate?

In my Phaser3 game there is a global gameTick variable that is incremented every update.在我的 Phaser3 游戏中,有一个全局 gameTick 变量,每次更新都会递增。 I am using this to spawn in enemies in my game every 100th update.我每 100 次更新就使用它在我的游戏中生成敌人。

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.这很好用,但是一旦用户在刷新率 >60hz 的显示器上玩游戏,更新时间就会中断,并导致外星人更频繁地生成。

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.我检查了this.physics.world.fps ,它是 60。我也可以修改this.physics.world.timescale ,但是我必须为每个刷新率做一个巨大的 switch 语句。

Either I am missing an obvious solution or my global.gameTick method is not an effective way to accomplish this task.要么我缺少一个明显的解决方案,要么我的global.gameTick方法不是完成这项任务的有效方法。

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,
};

My original post was entirely incorrect.我原来的帖子完全不正确。 this.physics.world.setFPS() only limits the update rate for the physics simulation. this.physics.world.setFPS()仅限制物理模拟的更新速率。

To actually 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.如果有 16.5 毫秒的延迟,它只会运行update()代码。

The example above works for 60fps, but if you want to limit the FPS to a different value use the formula: delay = 1000/fps .上面的示例适用于 60fps,但如果您想将 FPS 限制为不同的值,请使用公式: delay = 1000/fps

Original Post:原帖:


The FPS settings in the config seems to be bugged as of v3.21.0.从 v3.21.0 开始,配置中的 FPS 设置似乎已被窃听。

All I had to do was use this.physics.world.setFPS(60);我所要做的就是使用this.physics.world.setFPS(60); from my scene class.从我的场景课。 This limited the update rate perfectly and I am having no issues.这完美地限制了更新率,我没有问题。

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资料来源: https ://phaser.discourse.group/t/how-to-limit-fps-with-phaser-3/275/14?u=saricden

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM