javascript - 如何设置对撞机与世界边界(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 事件,如下所示:
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);
推荐阅读
- java - 如何从 Android 更新 DynamoDB 中的项目?
- hadoop - 如何在启用 kerberos 的 HDFS 中创建非超级用户
- c++ - clang-7:错误:链接器命令失败,退出代码为 1
- sql - 如何在其他列的基础上填充一列?
- gradle - 无法在 Kotlin 上导入 gradle 依赖项的类
- opencv - OpenCV-如何从相机旋转捕获的视频
- node.js - 对于处理 Sequelize 的方法的调用,这看起来很典型吗?
- python - Python pyqt5 使用默认参数和附加参数发送信号
- python-2.7 - 如何在 HAC 中对某些对象进行聚类,但它们具有相同的余弦相似度值
- docker - 即使没有容器映射到该端口,与 K8 pod 端口的连接是否会成功?