admin管理员组

文章数量:1332361

I have used Phaser 2 for a while but recently converted to Phaser 3 and I want to know if there is a method or member that might be equivalent to 'outOfBoundsKill'. I have an Arc Object in Phaser 3 and applied gravity to it and I want to make sure it is killed or destroyed when it is out of bounds of the canvas.

More about outOfBoundsKill: .6.2/Phaser.Sprite.html#outOfBoundsKill

I have tried this code sample and it did not destroy the arc object, 'ball' is the arc object.

ball.on('worldbounds', function() {
  if (!Over) {
    ball.destroy();

    HealthBar.livesLeft -= 1;
    HealthBar.cs.scale.x = HealthBar.livesLeft / HealthBar.lives;

    var shake = this.sound.add('shake');
    shake.play();
  }
}, this);

I have used Phaser 2 for a while but recently converted to Phaser 3 and I want to know if there is a method or member that might be equivalent to 'outOfBoundsKill'. I have an Arc Object in Phaser 3 and applied gravity to it and I want to make sure it is killed or destroyed when it is out of bounds of the canvas.

More about outOfBoundsKill: https://phaser.io/docs/2.6.2/Phaser.Sprite.html#outOfBoundsKill

I have tried this code sample and it did not destroy the arc object, 'ball' is the arc object.

ball.on('worldbounds', function() {
  if (!Over) {
    ball.destroy();

    HealthBar.livesLeft -= 1;
    HealthBar.cs.scale.x = HealthBar.livesLeft / HealthBar.lives;

    var shake = this.sound.add('shake');
    shake.play();
  }
}, this);

Share Improve this question edited Oct 11, 2019 at 21:03 Selorb 921 silver badge9 bronze badges asked Oct 31, 2018 at 20:56 user9814313user9814313
Add a ment  | 

1 Answer 1

Reset to default 9

There is no built in equivalent, that I could find, but I do know how to replicate it

const sprite = this.physics.add.sprite(x, y, 'key');

// Turn on wall collision checking for your sprite
sprite.setCollideWorldBounds(true);

// Turning this on will allow you to listen to the 'worldbounds' event
sprite.body.onWorldBounds = true;

// 'worldbounds' event listener
sprite.body.world.on('worldbounds', function(body) {
  // Check if the body's game object is the sprite you are listening for
  if (body.gameObject === this) {
    // Stop physics and render updates for this object
    this.setActive(false);
    this.setVisible(false);
  }
}, sprite);

Do not use destroy(). It is putationally expensive and requires that you recreate the object again (if there isn't anything pointing to it already).

本文标签: javascriptoutOfBoundsKill equivalent in Phaser 3Stack Overflow