Scope issue with collision

The function signature is as follows:
overlap(object1 [, object2] [, overlapCallback] [, processCallback] [, callbackContext])

You need to add the this as the fifth argument after adding the fourth argument with value null there after the callback function you passed. I guess, as I understand it is called processCallback. I tried to pull it out of docs, it is a bit confusing there imo.

this.physics.add.overlap(this.player,this.slimeE, function(player,slime){
		console.log('hit');
		if(slime.hostile){
			this.scene.start('menu');
		}
	}, null, this);

docs says for this processCallback here:

An optional callback function that lets you perform additional checks against the two objects if they collide. If this is set then collideCallback will only be called if this callback returns true .

and some same more here: Phaser 3 API Documentation - Class: World

It turns out it is useful. I mean it can cause to leaner tidier code, since it takes all the logic for the collision condition usually goes into the collideCallback in all examples I have ran across out of it. I will try to use it instead.

1 Like