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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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