Group of reused sprites, overlap sometimes triggered without actual overlap

I’ve uploaded my new game where you have to collect gems using the mouse to itch.io. But now I’ve noticed that, only sometimes, a gem is collected even though it is actually nowhere near the player sprite. :thinking:

I’m using a group of sprites that are reused, and an overlap sometime incorrect firing incorrect overlap.

After some debugging, I’ve found that when reusing sprite with group.get(x, y), the gem sprite.x .y values are updated, however the sprite.body.x .y are not at the new location yet and this triggers the overlap function. See code below

create: function ()
{
	// initialise everything

	this.physics.add.overlap(this._sprguy, this._grppower, this.doOverlapPowerup, null, this);
	//etc..
}

//..
doOverlapPowerup: function (smileyguy, powergem) {
	
	// score a point, sound effect, particles etc.
	//..

	// Hide the gem sprite and disable the body
	this._grppower.killAndHide(powergem);

	//  add new powerup
	this.addPowerUp();
},

addPowerUp: function () {
	// spawn at random position
	xpos = Phaser.Math.RND.between(20, GAME_WIDTH-20);
	ypos = Phaser.Math.RND.between(20, GAME_HEIGHT-20);

	// reuse inactive sprite or create new
	var newgem = this._grppower.get(xpos, ypos, "sprites", "gem1");

	// set it to active
	newgem.setActive(true);
	newgem.setVisible(true);
},

So in other words, when reusing a pool of sprites sometime (by coincidence) the old position overlap with the current player position, causing a “false positive” in overlap detection.

My question is this maybe a bug? Or am I missing something, and is there something I can do to prevent this?

Old conversation, but I’m working on a game and running into the same issue.

By setting debug in the game config I can verify that the bodies are sticking around until (I believe) getFirstDead() runs and grabs it.

After calling this.setActive(false).setVisible(false); I’m setting this.body.x = -100; and it’s at least moving it out of the way, but I’m curious if there’s a better way to handle this.

This part is normal. As long as you’ve disabled the bodies they won’t do anything.

With physics groups you should use enableBody(true, x, y) or body.reset(x, y) after group.get().

Doh.

Using this.disableBody() was what I was missing. And this.enableBody allowed me to simplify a bunch of logic as well.

Thanks samme!