gbrennon gbrennon - 10 months ago 81
Javascript Question

Phaser sprite.kill() sometimes doesn't work

I'm writing some games with phaser and I'm facing a problem with the method sprite.kill().

Sometimes when I call sprite.kill() Phaser seems to destroy the body for collisions/overlapping but the visual element(image and draggable object) still on screen.

I've set the overlapping method for all my objects through a foreach:

garbList.forEach(function(g) {
trashList.forEach(function(t) {
game.physics.arcade.overlap(g.sprite, t.sprite, garbHitTrash);

I have a garbageList and a trashList and they have a method for overlaping called garbHitTrash.

When garbHitTrash is called it just "throws the garbage on the trash" destroying its sprite and popping it from then garbList.

function garbHitTrash(garb, trash) {
if(garb.trash === trash.type) {
} else {
console.log('garbList.length ' + garbList.length);

Answer Source

If you need to completely remove your elements, have a look to the destroy method rather than kill. Calling kill is just a shorthand for setting some properties to false (like alive or visible). However, as your object still exist and is still moving (I suppose, due to the collision), those properties are likely to be set or reset by another component.