限制 Phaser3 更新率的方法?
Ways to limit Phaser3 update rate?
在我的 Phaser3 游戏中有一个全局 gameTick 变量,每次更新都会递增。我在每 100 次更新时使用它在我的游戏中生成敌人。
这是我的场景中发生的事情的简化示例class:
update () {
global.gameTick++;
if (global.gameTick % 100 === 0) {
this.spawnAlien();
}
}
这工作正常,但一旦用户在刷新率 >60hz 的显示器上玩游戏,更新时间就会中断并导致外星人更频繁地产生。
我已经检查过 this.physics.world.fps
并且它是 60。我也可以修改 this.physics.world.timescale
但是我必须为每个刷新率做一个巨大的 switch 语句。
要么我缺少明显的解决方案,要么我的 global.gameTick
方法不是完成此任务的有效方法。
这是我目前的配置
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,
};
要限制更新率,请使用以下方法。
// 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
}
}
这会累积最后一帧和当前帧之间的毫秒数。如果有 16.5 毫秒的延迟,它只会运行 update()
代码。
上面的示例适用于 60fps,但如果您想将 FPS 限制为不同的值,请使用公式:delay = 1000/fps
.
您还可以在游戏的配置对象中设置以下 属性:
fps: {
target: 24,
forceSetTimeOut: true
},
来源:https://phaser.discourse.group/t/how-to-limit-fps-with-phaser-3/275/14?u=saricden
在我的 Phaser3 游戏中有一个全局 gameTick 变量,每次更新都会递增。我在每 100 次更新时使用它在我的游戏中生成敌人。
这是我的场景中发生的事情的简化示例class:
update () {
global.gameTick++;
if (global.gameTick % 100 === 0) {
this.spawnAlien();
}
}
这工作正常,但一旦用户在刷新率 >60hz 的显示器上玩游戏,更新时间就会中断并导致外星人更频繁地产生。
我已经检查过 this.physics.world.fps
并且它是 60。我也可以修改 this.physics.world.timescale
但是我必须为每个刷新率做一个巨大的 switch 语句。
要么我缺少明显的解决方案,要么我的 global.gameTick
方法不是完成此任务的有效方法。
这是我目前的配置
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,
};
要限制更新率,请使用以下方法。
// 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
}
}
这会累积最后一帧和当前帧之间的毫秒数。如果有 16.5 毫秒的延迟,它只会运行 update()
代码。
上面的示例适用于 60fps,但如果您想将 FPS 限制为不同的值,请使用公式:delay = 1000/fps
.
您还可以在游戏的配置对象中设置以下 属性:
fps: {
target: 24,
forceSetTimeOut: true
},
来源:https://phaser.discourse.group/t/how-to-limit-fps-with-phaser-3/275/14?u=saricden