phaser-framework - 我怎样才能只放一次炸弹,直到它在物理街机池中爆炸?
问题描述
我想每 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 分钟放一个炸弹,直到它爆炸
解决方案
没有更多代码/信息,我只能猜测。
因为看起来你每次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 当然你可以隐藏炸弹并重生它们,但是按照这篇文章的建议最好的方法来摧毁一个物体我会建议首先尝试这种简单的方法。
推荐阅读
- reactjs - 使用 recharts 或 react-chartjs-2 使用 ReactJS 创建混合条形图
- json - 了解 JSON 迭代
- ruby-on-rails - QBXML SalesOrderAdd 问题
- reactjs - oidc-client.min.js 中的意外令牌“else”导致 Azure AD 登录后 React 应用程序刷新
- php - 在 URL 中隐藏 .php 会破坏联系表单
- javascript - 检查bucketKey的正则表达式只能包含:-_.a-z0-9
- javascript - React 类组件中的同步 Meteor 调用
- r - 如何在 gtsummary 中为“未知”添加百分比
- reactjs - 如何将用户登录信息传递给 Liferay 7.2 上运行的 React 应用程序?
- java - 线程使用共享类时条件放置在哪里