各位大佬们有没有比较优雅的节点移动方案?

比如说节点1从坐标A移动到坐标B

用向量做插值

CocosCreator3.x框架——节点扩展(仿2.x风格直接设置属性、渐变色、振动、实时销毁)

startPos = new Vec3(100, 100); // 起始坐标 A
endPos = new Vec3(400, 400); // 目标坐标 B
moveDuration = 2; // 移动时长(秒)

// 记录当前时间和移动的进度
elapsedTime = 0;

start() {
// 初始化节点位置
this.node.setPosition(this.startPos);
}

update(dt: number) {
this.elapsedTime += dt;
let progress = this.elapsedTime / this.moveDuration;
if (progress > 1) {
progress = 1;
}
const newPos = this.startPos.lerp(this.endPos, progress);
// 设置节点的新位置
this.node.setPosition(newPos);
// 移动完成后的操作
if (progress === 1) {
console.log(“移动完成”);
this.enabled = false;
}
}

tween就可以了

+10086

https://github.com/gameall3d/QuickTween

1赞