如何在Phaser Arcade物理系统中实现每X秒仅放置1枚炸弹至其爆炸?
实现方案
你只需要给Bomb类增加一个放置状态锁就可以实现需求,具体修改如下:
Bomb extends Phaser.Physics.Arcade.Group { constructor(physicsWorld, scene) { super(physicsWorld, scene) // 放置权限标记,默认允许放置 this.canPlace = true // 自定义间隔时长(单位毫秒,可自行修改X对应的数值) this.placeInterval = 3000 } newBomb(x, y){ // 没有放置权限时直接拦截,不生成炸弹 if (!this.canPlace) return let bomb = this.create(x, y, 'bomb') if(bomb){ // 生成炸弹后立刻锁定放置权限 this.canPlace = false bomb.setActive(true) bomb.setVisible(true) bomb.setImmovable(true) bomb.setOrigin(0) // 建议替换为Phaser自带的延时调用,和游戏生命周期同步更稳定,避免场景销毁后定时器仍执行的异常 this.scene.time.delayedCall(this.placeInterval, () => { bomb.disableBody(true, true) console.log('BOOM!') // 炸弹爆炸后解锁放置权限 this.canPlace = true }) } } }
补充说明
如果你习惯用原生setTimeout也可以保留原有写法,只需要补充状态锁的判断和赋值逻辑即可,核心逻辑不变。
内容的提问来源于stack exchange,提问作者Matias
相关产品推荐
相关产品推荐

