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.
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?