Need help with destroying my player upon collision

In my code I am trying to call .destroy(); on my player in a collision function but everytime I try to destroy my player or do anything like that in any of my functions the game freezes/crashes. Any ideas/help greatly appreciated. Also sorry if the code looks a bit messy I couldnt figure out how to properly upload the code snippit.

function create() {
cursors = this.input.keyboard.createCursorKeys();
player = this.physics.add.sprite(200, 200, ‘player’);

spikes =;

bullets ={
    allowGravity: false,

//when bullet touches spikes destroy that spike
this.physics.add.overlap(bullets, spikes, hitSpike, null, this);

//when player touches spike kill the spike
this.physics.add.overlap(player, spikes, killSpike, null, this);


//bullet destroy spike

function hitSpike (bullet, spike)
var crystal = crystals.create(spike.x, spike.y, ‘crystal’);


//kill player
function killSpike (player, spike) {

Check console tab in dev tools for any errors. You will get the hint on what is going wrong from error message.

For adding code snippets, wrap your code in between 3 backticks (```) at start and 3 backticks at end.

This was solved on Discord. Problem was

function update() {
  // …
  // …

which errors if the sprite has been destroyed (because the physics body is removed).

Solution is to handle that case in update() either with a special variable (gameOver) or testing the player sprite (!player.scene).

1 Like