AnimationComponent组件有一个奇怪的bug

假设我有一个预制件,预制件上有一个AnimationComponent组件。那么当我使用一下代码

let node = cc.instance(prefab);
node.getComponent(AnimationComponent).play('walk');
node.parent = this.node;

这段代码来创建节点并且希望播放一个动画的时候,会发现这个walk的动画并没有播放。这是因为在你调用 play函数的时候, AnimationComponet这个组件自己的start()方法还没有执行,导致你没法播放。只有在AnimationComponet的start方法执行完之后,play这个函数才会起作用

cocos3D 1.0.3正式版本

建议再prefab上挂一个脚本,在脚本的start方法中play

调换一下语句顺序试试
将节点添加到容器之后,节点才会onload和start
let node = cc.instance(prefab);
node.parent = this.node;

node.getComponent(AnimationComponent).play(‘walk’);

这个是因为你创建出来后没有添加到场景节点树里,所以不属于场景的一部分,就不会执行生命周期,你要先设置 parent