How to add Animation Events at runtime(Cocos Creator)

I’m trying to add Animation Events at runtime by code. I successfully added those events to the AnimationClip.events array, but when I play my game those Events were not called by my callback. When I added the events at the animation file it works. Is there any solution to add animation events to a animation clip and call them successfully?

Modifies to the AnimationClip will only work before AnimationState decode AnimationClip.
Gets AnimationState, play AnimationState will make AnimationState decode AnimationClip, so you need to modify AnimationClip before these operation.

Thanks for your reply, I try this solution.

Regards

monguskan, jyinkailej,

did you achieved what you asked?

I have the same question.
The reason for me is obvious: I can setup animation ended callback from editor, but that callback can fire event only to custom js component, which is attached to root node of an animation. I’ve got several custom js components, but I’ve got much more animations on nodes without js code attached and I do not want to attach custom js component to all of them just to know when their animation ended.

Can you help, please?

If you just need to know when animation ended, maybe this is what you want.

4 Likes

That was exactly what I want! Thank you a lot!
I see all this examples for the first time, how could I miss them while reading documentation :frowning: