Collider fires endless without colliding

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. :slight_smile:

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);
        }
    }
}

The collider will only fire when the bullet body is enabled.
Not sure why this is necessary, but you could put this after createMultiple:

    for (let i of this.getChildren()) i.body.enable = false;

And then when you fire, set this.body.enable = true.

2 Likes

You need to create bullets with physics bodies disabled. When enabled they can collide, it doesn’t matter if the sprite is active or not.

One way is

// AmmoGroup constructor()
super(scene.physics.world, scene, { enable: false });
1 Like

Disabling the body worked, thank you.

In the meantime I found the checkCollision function, which worked too, when I set checkCollision.none = true when creating my bullets and = false when firing.
However, it felt like kind of a dirty solution.