Jump to content

Kill a sprite with Collision


willdta
 Share

Recommended Posts

Using Phaser IO, I'm trying to kill off another sprite that's currently not in a collision detection.

I have a collision set with the player sprite and a treasure chest and what I want to do is on collision with the treasure chest, kill off another sprite to unlock a level but that sprite is not set in the collision.

How do you got about handle 3 way collisions where 1 sprite is not included with the collision?

The 3 items are player, treasure, and levelUnlock

player is colliding with treasure

 

game.physics.arcade.collide(player, treasure, this.spawnWeapon, null, this)
game.physics.arcade.collide(player, treasure, this.spawnLevel, null, this)
 
spawnWeapon: function(player, treasure) {
this.enableWeapon = true
treasure.kill()
}

 

spawnLevel: (player, levelUnlock) => {
if (this.enableWeapon) {
levelUnlock.kill()
}
 
 
Link to comment
Share on other sites

You just need to create and keep a reference to the third sprite somehow.

var player, treasure, levelUnlock;

var state = {
  create: function () {
    levelUnlock = this.add.sprite(/*…*/);
  },

  update: function () {
    game.physics.arcade.collide(player, treasure, this.collidePlayerVsTreasure, null, this);
  },
  
  collidePlayerVsTreasure: function(_player, _treasure) {
    levelUnlock.kill();
    _treasure.kill();
  }
}

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...