浏览器最小化,这时候update是停止的,会有问题吧?你的游戏我也玩了,最小化再回来,貌似游戏是暂停的。我是这么写的,渲染帧的帧数和逻辑帧无关,逻辑帧收到消息之后直接设置逻辑位置,然后根据移动速度和方向弧度设置一个逻辑帧移动所需耗费时间,然后渲染帧里面根据update的deltaTime,换算出渲染一次所需时间,然后进行lerp。我现在的表现是,浏览器最小化,逻辑帧继续跑,回复浏览器,渲染帧会直接设置人物位置到最新位置。以下是代码,不知道有没有帮到你:
逻辑帧update:
public update(delta: number) {
if (this.moveRadian != -9999) {
let oldPosition = this.position.clone();
let position = oldPosition.clone().add(this.getV3(this.moveRadian, delta));//目标位置
if (!this.battle.navmesh.isPointInMesh(position)) {//如果目标点是障碍
const dx = position.x - oldPosition.x;
const dy = position.y - oldPosition.y;
let findResult = this.battle.navmesh.findClosestMeshPoint(new Vector2(position.x, position.y), Math.sqrt(dx * dx + dy * dy));
if (!findResult.point) {
return;
}
let radian = Math.atan2(findResult.point.y - oldPosition.y, findResult.point.x - oldPosition.x);
position = oldPosition.clone().add(this.getV3(radian, delta));//重设目标位置
}
this.position = position;
this.direction = this.position.clone().subtract(oldPosition);
this.totalTime = this.direction.length() / this.moveSpeed;
this.walkTime = 0;
}
}
渲染帧update:
private update(delta: number) {
if (this.hero.walkTime >= this.hero.totalTime) {
return;
}
this.hero.walkTime += delta;
if (this.hero.walkTime >= this.hero.totalTime) {
this.hero.walkTime = this.hero.totalTime;
}
let ratio = Math.floor(this.hero.walkTime / this.hero.totalTime * 1000) / 1000;
this.node.position = this.node.position.lerp(this.hero.position, ratio);
}
我的邮箱392883202@qq.com