本人小白在学习COCO3.8.3,想要尝试复刻植物大战僵尸,目前在动画问题上遇到个问题求大佬们指点下。
就是在原本有头的僵尸被攻击到半血的时候,我想实现僵尸头掉了,但身体继续行走的效果
目前本人的做法是做了3个动画剪辑分别是[“ZombieMove”,“ZombieLostHeadMove”,“ZombieLostHead”]
其中"ZombieMove"是有头的僵尸移动动画
"ZombieLostHeadMove"是没有头的僵尸移动动画
"ZombieLostHead"是单纯的头掉了的动画
在instantiate(ZombiePrefab)后,自动循环"ZombieMove"动画
在僵尸半血后,播放”ZombieLostHeadMove“, 同时instantiate(ZombieHeadPrefab),并且根据僵尸位置setPosition,在ZombieHeadPrefab中播放"ZombieLostHead"动画.
虽然也能实现想要的,但感觉始终是野路子,怕以后要同时运行多个动画会有问题。因此请教各位大佬有没有其他优雅的方法实现。
具体代码如下,如果有值得改进的地方也请指出来,谢谢:
@ccclass('Zombie')
export class Zombie extends Enemy{
@property(Prefab)
head: Prefab = null; //僵尸头的预制体
start() {
super.start();
}
update(deltaTime: number) {
super.update(deltaTime);
}
deadAnim() {
const animation = this.getComponent(Animation);
if (animation) {
animation.play('ZombieDead');
}
animation.once(Animation.EventType.FINISHED, () => {
this.node.destroy();
});
}
halfLifeAnim() {
const animation = this.getComponent(Animation);
if (animation) {
animation.play('ZombieLostHeadMove');
let head = instantiate(this.head);
head.parent = find("Canvas/ForeGround");
let pos = this.node.getPosition().clone();
head.setPosition(pos.add(new Vec3(40, 40,0)));
let headAnim = head.getComponent(Animation);
headAnim.play();
headAnim.once(Animation.EventType.FINISHED, () => {
head.destroy();
});
}
}
}