我已经使用了Phaser 2一段时间,但最近将其转换为Phaser 3,我想知道是否有一种方法或成员可能等效于'outOfBoundsKill'。我在Phaser 3中有一个Arc Object,并对其施加了重力,因此我想确保在超出画布范围时将其杀死或破坏。
有关outOfBoundsKill的更多信息:https ://phaser.io/docs/2.6.2/Phaser.Sprite.html#outOfBoundsKill
我已经尝试过此代码示例,但它没有破坏弧对象,“ ball”是弧对象。
ball.on('worldbounds', function() {
if (!Over) {
ball.destroy();
HealthBar.livesLeft -= 1;
HealthBar.cs.scale.x = HealthBar.livesLeft / HealthBar.lives;
var shake = this.sound.add('shake');
shake.play();
}
}, this);Run Code Online (Sandbox Code Playgroud)
我没有找到等效的内置函数,但是我知道如何复制它
const sprite = this.physics.add.sprite(x, y, 'key');
// Turn on wall collision checking for your sprite
sprite.setCollideWorldBounds(true);
// Turning this on will allow you to listen to the 'worldbounds' event
sprite.body.onWorldBounds = true;
// 'worldbounds' event listener
sprite.body.world.on('worldbounds', function(body) {
// Check if the body's game object is the sprite you are listening for
if (body.gameObject === this) {
// Stop physics and render updates for this object
this.setActive(false);
this.setVisible(false);
}
}, sprite);Run Code Online (Sandbox Code Playgroud)
不要使用destroy()。它的计算量很大,并且需要您重新创建该对象(如果还没有指向它的对象)。