这是update中的代码,在window运行没什么问题,看着很流畅,在这个型号的苹果电脑上就看起来卡卡的,而且移动速度也变慢了,在苹果上的效果就和在window本地预览时,把fps调成20差不多 ,所以开始看帧率问题,但是如代码中判断机型后将游戏帧数设为20,但是没什么效果,所以我是应该修改那里?求大佬指导,感激不尽!
那你得看update中dt的信息 看window和mac有什么区别
反正第一感觉,就是2个系统 不同显示器设备的刷新率不同导致的
mac里的dt打印出来总体比window小点
应该是有影响,但我现在脑子乱到不知道从那块入手了,貌似每种方法都尝试过
以下是针对 Cocos Creator 3.8.x 版本的一些解决方案:
1. 使用固定时间步长
在 Cocos Creator 3.x 中,你可以使用固定时间步长来更新游戏逻辑,以确保在不同刷新率的显示器上,游戏逻辑的执行频率是一致的。
import { _decorator, Component, game } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('FixedUpdateExample')
export class FixedUpdateExample extends Component {
private fixedDeltaTime: number = 1 / 60; // 固定时间步长,例如 60 FPS
private accumulatedTime: number = 0;
update (dt: number) {
this.accumulatedTime += dt;
while (this.accumulatedTime >= this.fixedDeltaTime) {
this.fixedUpdate(this.fixedDeltaTime);
this.accumulatedTime -= this.fixedDeltaTime;
}
}
fixedUpdate (fixedDeltaTime: number) {
// 在这里执行你的游戏逻辑
}
}
2. 使用 requestAnimationFrame
你也可以使用 requestAnimationFrame 来确保在不同刷新率的显示器上,游戏逻辑的执行频率是一致的。
import { _decorator, Component, game } from 'cc';
const { ccclass, property } = _decorator;
@ccclass('RequestAnimationFrameExample')
export class RequestAnimationFrameExample extends Component {
private lastTime: number = 0;
private fixedDeltaTime: number = 1 / 60; // 固定时间步长,例如 60 FPS
onLoad () {
this.loop();
}
loop () {
requestAnimationFrame(this.loop.bind(this));
let now = performance.now();
let deltaTime = (now - this.lastTime) / 1000;
this.lastTime = now;
while (deltaTime >= this.fixedDeltaTime) {
this.fixedUpdate(this.fixedDeltaTime);
deltaTime -= this.fixedDeltaTime;
}
}
fixedUpdate (fixedDeltaTime: number) {
// 在这里执行你的游戏逻辑
}
}
3. 使用 game.frameRate
Cocos Creator 3.x 版本中,你可以使用 game.frameRate 来设置游戏的帧率。
import { game } from 'cc';
game.frameRate = 60; // 设置帧率为 60 FPS
4. 检查平台差异
你可以通过检查当前运行的平台,来针对不同平台进行不同的处理。例如:
import { sys } from 'cc';
if (sys.os === sys.OS.WINDOWS) {
// Windows 平台的处理逻辑
} else if (sys.os === sys.OS.OSX) {
// macOS 平台的处理逻辑
}
总结
通过以上方法,你可以在不同刷新率的显示器上确保 Cocos Creator 3.8.x 的 update 方法执行逻辑的一致性。希望这些建议对你有所帮助!如果你有更多具体的问题或需要进一步的帮助,请随时告诉我。
1赞
万分感谢,我试一试

