Jump to content

"Listener is a required param of add() and should be a Function."


Go to solution Solved by gsko,

Recommended Posts

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites

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 post
Share on other sites
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 post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...