onAnimationEnd not triggered when animation is reversed while it is running

I’m trying to reverse an already running animation, but the onAnimationEnd event doesn’t get triggered in this case.
What I’m basically trying to do is add a button with an animation (move in with alpha fade in effect from side). This button can be clicked during the animation, and the animation should reverse at the current frame. But I also need to know when the animation is done.

My idea was to simply set the speedRatio of the animatable to -1, which seems to work, but the event isn’t triggered anymore in this case.

Here is a simple playground example:
At frame 120 the animation is reversed using an animation event. If you remove the line “animation.addEvent(animationEvent);”, the text in the center shows “Animation finished”.

Is this a bug, or do I have to do something else to accomplish this?


Why not simple add one more frame in the midle of animation
like this you no more need to reverse, I kept your message only for showing.

About your issue, i don’t know if is a bug, or not, but I’ll tell you what are you doing in your playground
So you set first keyframe visibility to 0 and last keyframe to 1, but meanwhile in the midle of animation you reverse it, this means from 0.5 you go back to 0. I think because of this the end event is never triggered, the animation don’t meet the conditions to be completed

This won’t work unfortunately, because the user can click the button at any time during the animation, so the animation has to be reversable at any frame, the center used in playground was just an example. And it can also just play normally until it’s finished at the last frame.

Ok. Then maybe this is not the way to do. Maybe ActionManager is a better solution

Hi Vorion

You can use BABYLON.Animatable.masterFrame to reverse the animation at any frame :

https://playground.babylonjs.com/ts.html#3CARG0#4 (Click on the sphere to reverse).

Simply changing from and to like here should work just fine: Babylon.js Playground

I have stopped a bug with the end value of the reverse animation: value should be 0 whereas it is 1. This will be available in the next nightly.

@sebavan is correct. But to explain…

Remember current frame is often frame - 1.