首页 > 解决方案 > 如何设置对撞机与世界边界(Phaser)?

问题描述

我正在使用 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;
});

但这不起作用。

有任何想法吗?

标签: javascriptphaser-framework

解决方案


我为您找到了解决方案:

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

enemy.setCollideWorldBounds(true);

其次,转动敌人精灵的选项来监听WorldBound 事件,如下所示:

enemy.body.onWorldBounds = true;

第三和最后,设置“wordbounds”事件监听器并让敌人像这样消失:

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

推荐阅读