【问题标题】:How to set collider with world bounds (Phaser)?如何设置对撞机与世界边界(Phaser)?
【发布时间】:2020-02-09 15:25:59
【问题描述】:

我正在使用 Phaser.io

我刚刚学会了如何设置碰撞器功能:

this.physics.add.collider(enemies, platforms, function (enemy) {
  enemy.destroy();
  gameState.score += 10;
});

但我想在没有平台的情况下做同样的事情。而不是平台,我想使用世界边界。

我知道你可以这样设置世界边界:

player.setCollideWorldBounds(true);

我试过了:

this.physics.add.collider(enemies, this.worldBounds, function (enemy) {
  enemy.destroy();
  gameState.score += 10;
});

但这不起作用。

有什么想法吗?

【问题讨论】:

    标签: javascript phaser-framework


    【解决方案1】:

    我已经为你找到了解决方案:

    首先,将敌人的精灵设置为与 setCollideWorldBounds(true) 碰撞,如下所示:

    enemy.setCollideWorldBounds(true);
    

    第二,转动敌人精灵的选项来监听WorldBound events,如下所示:

    enemy.body.onWorldBounds = true;
    

    第三和最后,设置 "wordbounds" event listener 并让敌人像这样消失:

    enemy.body.world.on('worldbounds', function(body) {
      // Checks if it's the sprite that you'listening for
      if (body.gameObject === this) {
        // Make the enemy sprite unactived & make it disappear
        this.setActive(false);
        this.setVisible(false);
      }
    }, enemy);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-11
      • 2022-06-11
      • 1970-01-01
      • 1970-01-01
      • 2021-04-06
      • 2013-02-04
      • 2017-04-11
      相关资源
      最近更新 更多