[SOLVED] Detect if 'state' has changed?

Hi there, so I have this line

this.players[data.id].entity.children[0].animation.play(data.a);

data.a - Which returns data every frame, idle, walking etc (player animation)
The problem is, it starts animation over and over again instantly on every new frame
I need it to fire only once on every state change, since animations are looped and configured already
It has to detect state and prevent ‘animation.play’ until other state has arriwed
How to do it? Thanks

Before starting the animation you can check if data.a isn’t the current playing animation already. I think data.a is the animation state? I don’t know the exact code, but it should be something like below.

if (this.entity.anim.baseLayer.activeState !== data.a) {
    // start animation
}

Sorry, I think it should be a little different because you want to check it for the other players? As I can see on your code you use the old animation component for those entities?

Yes it’s an old component and yes it is for other players.

I think it swould be like this:

Lets say we start with data.a = ‘idle’
So we set animation.play(data.a) just once and monitor if data.a returns a new state, and repeat…

var laststate
var newtate

If laststate !== newstate
Animation.play newstate

Something like that, but yes, it should minotor state change and prevent firing anim play for the same states

If I understand the information for the post correctly, then you can probably do something like below.

if (this.players[data.id].entity.children[0].animation.data.currAnim !== data.a) {
    this.players[data.id].entity.children[0].animation.play(data.a);
}

Well that was simple, thanks!

1 Like