首页 > 解决方案 > 我怎样才能只放一次炸弹,直到它在物理街机池中爆炸?

问题描述

我想每 x 秒只放一颗炸弹

Bomb extends Phaser.Physics.Arcade.Group {
constructor(physicsWorld, scene) {
    super(physicsWorld, scene)
}

newBomb(x, y){

    let bomb = this.create(x, y, 'bomb')

    if(bomb){
        bomb.setActive(true)
        bomb.setVisible(true)
        bomb.setImmovable(true)
        bomb.setOrigin(0)

        setTimeout(()=> {
          bomb.disableBody(true, true)
          console.log('BOOM!')
        }, 3000)
    }
}

该出版物告诉我,我必须写更多,否则我将无法发布此消息-.-我唯一想要的就是每 x 分钟放一个炸弹,直到它爆炸

标签: phaser-framework

解决方案


没有更多代码/信息,我只能猜测。

因为看起来你每次newBomb调用函数时都在创建一个新的炸弹。我只想使用函数destroy,这应该从场景中删除对象。

我会写这样的东西:

class SimpleLevel extends Phaser.Scene {
    bombs;
    constructor() {
        super("SimpleLevel");
    }

    create() {
        // ... here comes the usual init and load code ...
        this.bombs = this.physics.add.staticGroup();
        this.addNewBomb(100, 100); // some Coordinates
    }

    addNewBomb(x, y) {
        let bomb = this.physics.add.image(x, y, "bomb");
        this.bombs.add(bomb);
        setTimeout(() => bomb.destroy(), 3000);
    }

    update(time, delta) {
        if (this.bombs.getLength() == 0) // when the group is empty create new bomb
            this.addNewBomb(100, 100); // some Coordinates
    }
}

详细信息在这里https://newdocs.phaser.io/docs/3.55.2/Phaser.GameObjects.Sprite#destroy 当然你可以隐藏炸弹并重生它们,但是按照这篇文章的建议最好的方法来摧毁一个物体我会建议首先尝试这种简单的方法。


推荐阅读