Phaser 3 collider fires endless

I try to make a bouncing bullet for a game that should disappear after bouncing 3 times. I thought it's simple, I gave my bullet a variable with the value of 3 and decreased it in my bullet-platform collider by one.

After it didn't work and checked the variable with console.log(), I found out that the variable gets decreased continuously. I tested my other colliders and found out that only the bullet-platform collider do that, while the player-platform, enemy-platform, bullet-player and bullet-enemy collider don't. In addition my function for destroying bullets, when they hit a platform, oddly work fine. My bullets only get destroyed when hitting a platform.

Does anyone have an Idea how to fix that?

Edit: I made a Github repository of my game for better insight. (At least I hope I did, I haven't used Github before.) https://github.com/Kiroho/Game

Edit2: After testing around, I found out that it must have something to do with how I use my ammoGroup class.

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.

My bullet-platform collider

this.scene.physics.add.collider(this.scene.platforms, this.scene.playerProjectiles_Bounce, function (platform, projectile) {
        //destoryProjectile(projectile);
        console.log("hit");
    });

The Ammo Groups. I use them as a "magazine" for each weapon

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']
    })
}

}

The bullet class

class Granade extends Phaser.Physics.Arcade.Sprite {
constructor(scene, x, y) {
    super(scene, x, y, 'kugel');
    this.dmg = 20;
    this.enemyHit = [];
    this.bounceCounter = 3;
    
}

fire(x, y, direction) {
    this.body.reset(x, y);
    this.body.setGravityY(-1000);
    this.setBounce(1);
    this.setActive(true);
    this.setVisible(true);

    this.setVelocityY(WeaponConst.VELOCITY_Y_GRENADE);
    if (direction == "left") {
        this.setVelocityX(WeaponConst.VELOCITY_X_GRENADE * -1);
    }
    else if (direction == "up") {
        this.setVelocityY(WeaponConst.VELOCITY_Y_GRENADE * 2);
        this.setVelocityX(0);
    }
    else {
        this.setVelocityX(WeaponConst.VELOCITY_X_GRENADE);
    }

}

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

}

Additional: The player (and the enemies) has a weapon object. This weapon object has a ammoGroup object and set it's projectile, the velocity when shooting, assigning it to groups and so on.

class Weapon {
constructor(scene) {
    this.scene = scene;
    this.type = null;
    this.attackRate = null;
    this.attackRange = null;
    this.ammoGroup = new AmmoGroup(this.scene.scene, 1)
    this.direction = null;
}


chooseWeapon(type) {
    if (type == 'Blaster') {
        this.setUpWeapon(
            WeaponConst.TYPE_BLASTER,
            WeaponConst.ATK_RATE_BLASTER,
            WeaponConst.VELOCITY_X_BLASTER,
            WeaponConst.VELOCITY_Y_BLASTER,
            WeaponConst.AMMO_GROUP_BLASTER
        );
        console.log("Blaster choosen");
        this.assignToGroup(this.scene.scene.playerProjectiles_Normal);

    }
    else if (type == 'BlasterBig') {
        this.setUpWeapon(
            WeaponConst.TYPE_BLASTER_BIG,
            WeaponConst.ATK_RATE_BLASTER_BIG,
            WeaponConst.VELOCITY_X_BLASTER_BIG,
            WeaponConst.VELOCITY_Y_BLASTER_BIG,
            WeaponConst.AMMO_GROUP_BLASTER_BIG
        );
        console.log("BlasterBig choosen");
        this.assignToGroup(this.scene.scene.playerProjectiles_PierceEnemies);

    }
    else if (type == 'Grenade') {
        this.setUpWeapon(
            WeaponConst.TYPE_GRENADE,
            WeaponConst.ATK_RATE_GRENADE,
            WeaponConst.VELOCITY_X_GRENADE,
            WeaponConst.VELOCITY_Y_GRENADE,
            WeaponConst.AMMO_GROUP_GRENADE
        );
        console.log("Grenade choosen");
        this.assignToGroup(this.scene.scene.playerProjectiles_Bounce);

    }
    else if (type == 'GrenadeBig') {
        this.setUpWeapon(
            WeaponConst.TYPE_GRENADE_BIG,
            WeaponConst.ATK_RATE_GRENADE_BIG,
            WeaponConst.VELOCITY_X_GRENADE_BIG,
            WeaponConst.VELOCITY_Y_GRENADE_BIG,
            WeaponConst.AMMO_GROUP_GRENADE_BIG
        );
        console.log("GrenadeBig choosen");
        this.assignToGroup(this.scene.scene.playerProjectiles_PierceEnemies);

    }
    else if (type == 'Laser') {
        this.setUpWeapon(
            WeaponConst.TYPE_LASER,
            WeaponConst.ATK_RATE_LASER,
            WeaponConst.VELOCITY_X_LASER,
            WeaponConst.VELOCITY_Y_LASER,
            WeaponConst.AMMO_GROUP_LASER
        );
        console.log("Laser choosen");
        this.scene.scene.enemyProjectiles_Normal.add(this.ammoGroup);
        this.scene.scene.enemyProjectiles_PiercePlayer.remove(this.ammoGroup);
        this.scene.scene.enemyProjectiles_PierceAll.remove(this.ammoGroup);
    }

}


setUpWeapon(type, attackRate, velocityX, velocityY, ammoGroup ) {
    this.type = type;
    this.attackRate = attackRate;
    this.velocityX = velocityX;
    this.velocityY = velocityY;
    this.ammoGroup.loadAmmo(ammoGroup);
    //this.scene.scene.enemyProjectiles_Normal.add(this.ammoGroup);
}


fire(x, y, direction) {
    this.ammoGroup.fire(x, y, direction);

}


assignToGroup(group) {

    this.scene.scene.playerProjectiles_Normal.remove(this.ammoGroup);
    this.scene.scene.playerProjectiles_PierceEnemies.remove(this.ammoGroup);
    this.scene.scene.playerProjectiles_PierceAll.remove(this.ammoGroup);
    this.scene.scene.playerProjectiles_Bounce.remove(this.ammoGroup);

    group.add(this.ammoGroup)
}

}



from Recent Questions - Stack Overflow https://ift.tt/3wo4EfM
https://ift.tt/eA8V8J

Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations