【发布时间】:2022-10-23 13:01:43
【问题描述】:
我正在尝试将我的子弹变成group。我是 Phaser 3 的初学者。我的子弹从here 开始工作。我想让创建的子弹可追溯。
还有一个问题。经过一些简短的测试后,我发现创建了多个项目符号。测试1:我在场景外部添加了障碍物,并添加了与子弹和障碍物的碰撞器,触发时它会记录多次。测试2:在与敌人接触时,一些子弹被反射并飞向另一个方向。这与问题有关,因为如果有某种方法可以追踪它们,这可以通过限制子弹来完成。
点击here查看replit的完整代码。
this.input.on('pointerdown', pointer => {
if (ammo > 0) {
charge.setText('CHARGED!');
let speed = 750;
console.log(ammo);
// create bullet
bullet = this.add.image(playerArm.x, playerArm.y, 'bullet');
bullet.setScale(0.5);
bullet.rotation = playerArm.rotation;
this.physics.add.existing(bullet);
group = this.add.group({
defaultKey: 'bullet',
maxSize: 100,
})
// get Vector where to shoot bullet
let vector = new Phaser.Math.Vector2(pointer.x - playerArm.x, pointer.y - playerArm.y);
// set Speed of bullet
vector.setLength(speed * timeSpeed);
// QuickFix to destroy the bullet after 1 Second automatically
setTimeout(() => bullet.destroy(), 1500);
ammo -= 1;
// to shoot in a straightline, just comment the following line in
// bullet.body.setAllowGravity(false);
function killBullet() {
bullet.destroy();
}
bullet.body.setVelocity(vector.x, vector.y);
bullet.body.setAllowGravity(false);
bulletsAlive.setText('Bullets alive' + group.getLength())
this.physics.add.collider(bullet, enemy, hitEnemy, null, this);
this.physics.add.collider(bullet, platforms, killBullet, null, this);
}
});
if (ammo <= 0) {
function onEvent() {
ammo = 15;
}
timedEvent = this.time.delayedCall(2000, onEvent, [], this);
// setTimeout(() => ammo = 15, 1000);
charge.setText('OUT OF CHARGE! CHARGING!');
console.log('reloading');
}
【问题讨论】:
-
我只是想问一下,我的回答有帮助吗,还是我错过了什么?
-
对不起,伙计,忘了批准它;-;。你的回答肯定有帮助!
标签: javascript phaser-framework