How to check overlap of object with any objects?

Phaser 3.24.1, Arcade physics

Hello there,

I’ve added overlap checking for object player, but i have questions:

 this.enemies = this.physics.add.group({
                key: 'enemy',
                repeat: 11,
                setXY: { x: 12, y: 0, stepX: 70 }
            });
  this.physics.add.overlap(this.player, this.enemies, () => {
                console.log('overlap');
            }, null, this);

setInterval(() => {
            this.enemies = this.physics.add.group({
                key: 'enemy',
                repeat: 11,
                setXY: { x: 12, y: 0, stepX: 70 }
            });

  this.physics.add.overlap(this.player, this.enemies, () => {
                console.log('overlap');
            }, null, this);
            }, 5000);

This code works only with first wave of enemies.
Overlap not working for second wave, so I add overlap each new wave of enemies.
How to fix this?

How to add trigger to check overlap for each object?
For example - player can shoot. I think - it is not good solution to add overlap listener to each bullet =)

Can you post the code that you were using before? Are you creating separate groups for each wave?

I attached file with this scene. Could you take a look please?

scene.js (2.8 KB)

Phaser 3 Scenes appear to have timer functionality that can work as you are trying to do:


Instead of creating a new Group every interval, can you just create a new batch of images (11 in your case) and add them to the already existing Group?

Edit
You may eventually need to get into Object Pooling so you aren’t creating too many enemies (if that is a concern for you).

1 Like

Remove the setInterval(…).

That’s creating a new group each time but overwriting the reference (this.enemies).

Set up a timer event and use this.enemies.createMultiple(…) instead.

1 Like