Collider callback syntax

I’m putting in a series of colliders and want to know if I can use an object method as a physics callback. Can I do this:

this.player.weapons.list.forEach(weapon => {
this.physics.add.overlap(
this.botSectionPool,
weapon.bullets,
this.weapons.bulletCollide,
this.weapons.bulletCheckIfCollide,
this);
}, this);

weapons.bulletCollide and weapons.bulletCheckIfCollide are both set up to receive a botSection and a bullet as arguments, but this syntax results in no collisions. Or must I do something like:

this.player.weapons.list.forEach(weapon => {
this.physics.add.overlap(
this.botSectionPool,
weapon.bullets,
(botSection, bullet) => {this.weapons.bulletCollide(botSection, bullet)},
(botSection, bullet) => {this.weapons.bulletCheckIfCollide(botSection, bullet)},
this);
}, this);

The first one feels neater, but doesn’t seem to work! And I think I’m having issues with scope for the second :frowning:

Thanks, Gordon

if i’ve understood correctly then you could use https://github.com/mikewesthad/phaser-matter-collision-plugin to setup the collision and use it to run the functions when it triggers

1 Like

Thanks, but using arcade physics rather than matter :frowning:

What i do is, put all my weapons before in an array, and add a overlap like:

this.physics.add.overlap(
this.botSectionPool,
weaponArray,
this.weapons.bulletCollide,
this.weapons.bulletCheckIfCollide,
this);
}, this);

If i understand well what you want…

1 Like

Probably the callbackContext should be this.weapons.

2 Likes

Thank you - just realised when I changed this that it should have been this.player.weapons in my callbacks as well as scope, which has fixed it :slight_smile:

Does this work for you? I tried adding all the weapons’ bullet groups to an array and colliding them before going with the forEach method, but then discovered apparently you can only collide arrays of physicsGroups, not regular groups. However looking again I’ve seen that when you create a weapon you can optionally add a group that all the bullets are added to, so I’ve created an allBullets group as a property of my weaponManager ‘weapons’ and collided that instead. Now looking much more elegant :slight_smile:

this.physics.add.overlap(
this.botSectionPool,
this.player.weapons.allBullets,
this.player.weapons.bulletCollide,
this.player.weapons.bulletCheckIfCollide,
this.player.weapons);