版本2.3.3。刚入门研究CCC,跟着官方教程做了一遍那个入门小游戏(小怪物摘星星)。原教程里是用Action
控制怪物跳跃,然后在update()
里控制左右平移。
新版CCC在运行时提示Action
即将被废弃,推荐使用tween
来做缓动动画,于是用tween
重写了缓动部分 (TypeScript代码):
cc.tween(this.node)
.repeatForever(
cc.tween()
.by(this.jumpDuration, { position: cc.v2(0, this.jumpHeight) }, { easing: 'cubicOut' })
.by(this.jumpDuration, { position: cc.v2(0, -this.jumpHeight) }, { easing: 'cubicIn' })
.call(() => this.playJumpSound())
)
.start();
但是在运行时发现update()
函数里更新node.x
的代码不能正常工作:
update(dt: number) {
// ...
// 上面是计算 this.xSpeed 的代码
this.node.x += this.xSpeed * dt;
}
打印this.node.x
发现 x
的值没有正常累加,感觉是 tween
的位移计算重置了 this.node.x
的值?打印出来的值大概是这样( x1 = 本帧修改前的this.node.x,v = xSpeed * dt,x2 = x1 + v ):
...
x1: 0, v: 6.667600000000676, x2: 6.667600000000676
x1: 0, v: 6.653600000000005, x2: 6.653600000000005
x1: 0, v: 6.687199999999575, x2: 6.687199999999575
...
请问有大佬知道为什么会这样吗?用tween
的时候如何正确更新node
的位置?