Question About The 'animationcomplete' Handler

I don’t understand why the argument sprite works in the function below. Why do you have to pass that to retrieve the key that triggered the event, and where is the data coming from?

var character = this.add.sprite(x, z, 'character');
character.play('character_create');

character.on('animationcomplete', function (sprite)
{
  if (sprite.key === 'character_create')
  {
     character.play('character_repeat');
  }
}, this);

I see that it works great for doing a comparison on what the current key, but I don’t know at all why it works after reading the documentation. Why wouldn’t character.key work?

The first argument to the callback is actually animation, which has animation.key. There is no character.key (sprites don’t have a key property).

For all events, the event emitter passes the arguments. You just pass a function to receive them.

1 Like

Thank you for the explanation. It seems that in JavaScript that you can pass more parameters than is declared in the function after looking it up. TIL. I never would have guess this unless you told me this behavior.

1 Like