Using physics.add.overlap in a Class

Hello All!

I’m learning how to package game objects into classes. I have a fish game object that has a sort of ‘event handler’ tied to an overlap when it collides with a player game object. When I was refactoring, I decided to take this functionality and put it in the constructor. I am getting an error that I can’t shake! The error is on Fish:22, and GameScene:32.

I have my github repo linked here:

Any help would be appreciated!
Thanks in advance!

The error message is odd but I think the problem is using this as a function parameter. this is a keyword and can’t be assigned to. Likely you want something like

this.scene.physics.add.overlap(this.target, this, (target, _this) => {
  // _this.fishPosition etc.
  // _this.scene.fishGrab
});

Or you could use fish for _this, if you find that clearer.

I usually add colliders in the scene itself and then call methods on the colliding objects, if necessary, but it’s a matter of preference.

Thanks samme. I’ll start putting my collision overlap “detection” in the scenes where they might happen. Thanks for the prompt response!