Skip to content Skip to sidebar Skip to footer

Typeerror: Undefined Is Not A Function (phaser Js)

Hi everybody i'm just trying executing a method inside a class and it's not working. I got an 'Uncaught TypeError: undefined is not a function' error. I'm calling others function

Solution 1:

"In fact, collisionBlocs() is a callback function from a phaser collision events : game.physics.arcade.collide(this.sprite, blocs, this.collisionBlocs); Maybe that's the problem"

That will be the problem. In JS, the value of this within a function depends on how a function is called. You pass a reference to collisionBlocs to the .collide() method and when it calls it it won't be setting this correctly so then this.bounce will be undefined.

You need to force the value of this to be correct. The easiest way to do that is with .bind():

game.physics.arcade.collide(this.sprite, blocs, this.collisionBlocs.bind(this));

The .bind() method is not supported in older browsers (IE <=8), but there is a polyfill you can use if you need to support those browsers.

MDN has more information on how this works in JavaScript.

Solution 2:

Rather than binding this it would be easier to use the callbackContext parameter that collide offers you. It was added for specifically this issue. Here's the method signature:

collide: function (object1, object2, collideCallback, processCallback, callbackContext)

You're not using processCallback so you can pass null for that, but you do need the context. This is the context in which the callback will be invoked (try this to start with).

Post a Comment for "Typeerror: Undefined Is Not A Function (phaser Js)"