如何设置具有世界边界的对撞机(Phaser)?
How to set collider with world bounds (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;
});
但这行不通。
有什么想法吗?
我已经找到适合你的解决方案:
首先,像这样设置你的敌人的精灵与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);
我正在使用 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;
});
但这行不通。
有什么想法吗?
我已经找到适合你的解决方案:
首先,像这样设置你的敌人的精灵与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);