On collision with a layer delete only the block you touch

Hi everyone.

I am making a little mario clone game and I have been stuck on this one problem for a while. I want to make it so that when the player jumps against a questionmark block that it disappears (mostly for testing purpose for now). I added the command

game.physics.arcade.collide(player, shroomblocks, function(){spawnMushroom(shroomblocks);});

(player being the player, shroomblocks being the shroomblock and in the spawnMushroom function it just spawns a mushroom and executes the command shroomblock.kill();. Now it works fine but the problem is it deletes ALL questionmark blocks and not just the one the player collides with. Anyone could help me out?

I also tried to put something like this.shroomblocks as parameter into the function but it just gives me an error saying its undefined.

Thanks in advance for the help and if any more info is needed just ask away.

Hi,

You can make it work by adding an overlap instead, in create :

this.physics.add.overlap(player, shroomblocks, spawnMushroom, null, this);

Your function that handle the overlap will receive 2 parameters : first the player sprite, then the block that was touched :

function spawnMushroom (player, block) {
   //do something with player
   //do something with block
   block.disableBody(true, true);
}

I tried putting your command in the create function and setting the 2 parameter in the spawnMushroom function but it gives me the error message “TypeError: Cannot read property ‘overlap’ of undefined phaser.min.js:20” in the console. Any ideas as to why?

Also could you explain what null and this do in the command you sent please?

Excuse me, can you confirm we are talking about Phaser 3 ? I’m also assuming you are using Arcade physics, tell me if it’s not the case.

oh damn my bad. I was using phaser 3 but then went back on a backup file of the game where i still had phaser 2.6.2 and didnt change it again. I will get phaser 3 and try again and tell you later if it works or not.

And yes im using the arcade physics just.

Edit: Actually i just saw i would have to rewrite most of the code and since I cant find a good documentation about how phaser 3 works I would rather keep my project on phaser 2.6.2 sorry again for the confusion. So can you help me with phaser 2.6.2 with arcade physics or should I open a new topic for phaser 2?

No problem, don’t worry.
To get help on Phaser 2 you should open a new topic in the other forum. It may help somebody else with the same question.

ok thanks for the help. gonna try my luck there