Hi,
I tried to make a bouncing bullet for a game that should disappear after bouncing 3 times. I gave my bullet a variable with the value of 3 and decreased it in my bullet-platform collider by one.
However, after testing around, I found out that it must have something to do with how I create my ammoGroup class.
I use a class “ammoGroup” that extends Physics.Arcade.Group to create multiple bullets (Arcade.Sprite objects)
I found out that, if I create bullets via ammoGroup (via createMultiple), the collider starts firing for each bullet until I shot them at least once.
After all existing bullets are fired once, everything works normally and the collider fires as intended → only on collision.
I started with Phaser only two weeks ago an have no idea what to do. I would appreciate any kind of advice.
The project on Github: GitHub - Kiroho/Game
My ammoGroup class
class AmmoGroup extends Phaser.Physics.Arcade.Group {
constructor(scene) {
super(scene.physics.world, scene);
}
fire(x, y, direction) {
const projectile = this.getFirstDead(false);
if (projectile) {
projectile.fire(x, y, direction);
}
}
loadAmmo(ammoIndex) {
this.clear();
if (ammoIndex == 1) {
this.classtype = Blaster;
}
else if (ammoIndex == 2) {
this.classtype = BlasterBig;
}
else if (ammoIndex == 3) {
this.classtype = Granade;
}
else if (ammoIndex == 4) {
this.classtype = Granade;
}
else if (ammoIndex == 0) {
this.classtype = Laser;
}
this.createMultiple({
classType: this.classtype,
frameQuantity: 20,
active: false,
visible: false,
key: ['ballAnim', 'kugel']
})
}
}
My bullet class
class Blaster extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
super(scene, x, y, 'ballAnim');
this.dmg = 25;
this.enemyHit=[];
//Animation in eigene Klasse auslagern
this.scene.anims.create({
key: 'wobble',
frames: this.anims.generateFrameNumbers('ballAnim', { start: 0, end: 1 }),
frameRate: 10,
repeat: -1
});
this.anims.play('wobble', true);
}
fire(x, y, direction) {
this.body.reset(x, y);
this.body.setGravityY(-3000);
this.setActive(true);
this.setVisible(true);
if (direction == "left") {
this.setVelocityX(WeaponConst.VELOCITY_X_BLASTER * -1);
}
else if (direction == "up") {
this.setVelocityY(WeaponConst.VELOCITY_X_BLASTER * -1);
}
else {
this.setVelocityX(WeaponConst.VELOCITY_X_BLASTER);
this.setVelocityY(WeaponConst.VELOCITY_Y_BLASTER);
}
}
preUpdate(time, delta) {
super.preUpdate(time, delta);
if (!this.scene.cameras.main.worldView.contains(this.x, this.y)) {
this.enemyHit = [];
this.setActive(false);
this.setVisible(false);
}
}
}