【问题标题】:How can I put a bomb only once until it explodes in the physics arcade pool?我怎样才能只放一次炸弹,直到它在物理街机池中爆炸?
【发布时间】:2021-12-25 03:53:55
【问题描述】:

我只想每 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


【解决方案1】:

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

因为每次调用函数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 当然,您可以隐藏炸弹并重生它们,但是按照这篇文章 best way to destroy an object 的建议,我建议您首先尝试这种简单方法。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-02
    • 1970-01-01
    • 2017-08-30
    • 2022-01-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多