outOfBoundsKill Equivelant в Phaser 3 - PullRequest
0 голосов
/ 31 октября 2018

Я использовал Phaser 2 некоторое время, но недавно преобразовал в Phaser 3, и я хочу знать, существует ли метод или элемент, который может быть эквивалентен 'outOfBoundsKill'. У меня есть объект Arc в Phaser 3, и я применил к нему гравитацию, и я хочу убедиться, что он убит или уничтожен, когда выходит за пределы холста.

Подробнее о outOfBoundsKill: https://phaser.io/docs/2.6.2/Phaser.Sprite.html#outOfBoundsKill

Я попробовал этот пример кода, и он не разрушил объект дуги, 'ball' - это объект дуги.

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);

1 Ответ

0 голосов
/ 06 ноября 2018

Нет встроенного эквивалента, который я мог бы найти, но я знаю, как его воспроизвести

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);

Не использовать destroy(). Это вычислительно дорого и требует повторного создания объекта (если на него уже ничего не указывает).

...