What? Posted December 9, 2014 Share Posted December 9, 2014 Yesterday I used onComplete to call a function after an animation finished playing. Today I ran the game and this error:Error: Phaser.Signal: listener is a required param of add() and should be a Function.The function still gets called and does its thing, but the game just stops because of the error. This didn't happen yesterday. The odd thing is that I do have a listener as parameter, and it is a function. My code: wait_for_animation: function(name,framerate) { playerState = 0; player.body.velocity.x = 0; player.animations.play(name, framerate, false); player.animations.currentAnim.onComplete.add(this.change_state(1), this); }, change_state: function(x) { //console.log("it works"); playerState = x; },The "wait_for_animation" function is being called once, not multiple times. What this stuff does is stops the player and plays a character animation. When the animation is done, the player can move again. When I comment out the line that adds the listener, the error doesn't come up. Link to comment Share on other sites More sharing options...
gsko Posted December 9, 2014 Share Posted December 9, 2014 I seem to remember having an error doing something similar. Maybe try:player.animations.currentAnim.onComplete.add(change_state, {x: 1});I'm a newb but really enjoying working with Phaser. Link to comment Share on other sites More sharing options...
What? Posted December 9, 2014 Author Share Posted December 9, 2014 I seem to remember having an error doing something similar. Maybe try:player.animations.currentAnim.onComplete.add(change_state, {x: 1});I'm a newb but really enjoying working with Phaser. Huh, it worked. I trust you can explain what's going on here? Link to comment Share on other sites More sharing options...
trueicecold Posted December 9, 2014 Share Posted December 9, 2014 the add function takes 2 parameters: callback - the function to call. This has to be the function reference, and not the function itself.context - the context to be passed to the function reference. It will be the only parameter passed to referenced function. That being said, you CAN write the function itself inside the first parameter like this:player.animations.currentAnim.onComplete.add(function() {this.change_state(1)}, this);but this is not recommended at all. Best practice is to use function reference only. Link to comment Share on other sites More sharing options...
What? Posted December 9, 2014 Author Share Posted December 9, 2014 the add function takes 2 parameters: callback - the function to call. This has to be the function reference, and not the function itself.context - the context to be passed to the function reference. It will be the only parameter passed to referenced function. That being said, you CAN write the function itself inside the first parameter like this:player.animations.currentAnim.onComplete.add(function() {this.change_state(1)}, this);but this is not recommended at all. Best practice is to use function reference only. Ok. I'm guessing that what gsko suggested was the correct way to write it. I'm still a bit of a novice at scripting. Thanks. Link to comment Share on other sites More sharing options...
Wavertron Posted December 10, 2014 Share Posted December 10, 2014 this.change_state() //This is invoking/calling the function this.change_state(1) //This is invoking/calling the function with a parameter of value 1 this.change_state //This is a reference to the function. It doesnt get invoked straight away. //You give a reference for a callback function to something, so that when that something is done it can go "ok great, I'm done, now I'll invoke change_state()" Link to comment Share on other sites More sharing options...
What? Posted December 10, 2014 Author Share Posted December 10, 2014 this.change_state() //This is invoking/calling the functionthis.change_state(1) //This is invoking/calling the function with a parameter of value 1this.change_state //This is a reference to the function. It doesnt get invoked straight away. //You give a reference for a callback function to something, so that when that something is done it can go "ok great, I'm done, now I'll invoke change_state()" I had no idea about that last one. That explains a lot of frustration I've had with callback functions. Link to comment Share on other sites More sharing options...
Recommended Posts