【问题标题】:outOfBoundsKill equivalent in Phaser 3Phaser 3 中的 outOfBoundsKill 等效项
【发布时间】:2021-01-01 08:17:28
【问题描述】:

我已经使用 Phaser 2 一段时间,但最近转换为 Phaser 3,我想知道是否有可能等效于“outOfBoundsKill”的方法或成员。我在 Phaser 3 中有一个 Arc Object 并对其施加了重力,我想确保它在超出画布范围时被杀死或销毁。

更多关于 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);

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    没有内置的等价物,我可以找到,但我知道如何复制它

    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()。它的计算成本很高,并且需要您再次重新创建对象(如果没有任何东西指向它)。

    【讨论】:

    • 应该有更好的方法来做到这一点。
    • 在检查了文档和示例之后,我认为这是目前正确的解决方案。
    猜你喜欢
    • 2015-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-22
    • 2012-06-19
    • 1970-01-01
    相关资源
    最近更新 更多