对于大学,我必须创建一个游戏,我的游戏涉及掉落的物体。但我不知道当物体超出范围时如何失去生命。
这是我的一些代码:
//my variables
var player;
var lives;
var max_lives = 3;
var objects;
var totalLife
var lifeText;
var totalScore;
var scoreText;
var inputs;
//creates the group for the objects
objects = this.add.group();
this.physics.enable(objects, Phaser.Physics.ARCADE);
objects.setAll('outOfBoundsKill', true);
objects.setAll('checkWorldBounds', true);
//spawns object
spawnObject: function () {
var random = this.rnd.integerInRange(0, 25);
if (random === 0) {
var randomX = this.rnd.integerInRange(0, this.world.width - 150);
var object = objects.create(randomX, -50, 'object');
this.physics.enable(object, Phaser.Physics.ARCADE);
object.body.velocity.y = this.rnd.integerInRange(200, 300);
}
}
最佳答案
Sprite 离开世界边界时会触发一个事件。听听它会掉一个人的命。
启用正在发出的事件:
sprite.checkWorldBounds = true;
然后,您可以收听:
sprite.events.onOutOfBounds
...并通过放弃一个生命来对此使用react。
关于javascript - Phaser 2 Sprite 检测边界?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23632645/