微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Phaser 3 对撞机无限火力

如何解决Phaser 3 对撞机无限火力

我尝试为一款弹跳 3 次后应该消失的游戏制作弹跳子弹。 我认为这很简单,我给了我的子弹一个值为 3 的变量,然后在我的子弹平台对撞机中将其减一。

在它不起作用并使用console.log()检查变量后,我发现变量不断减少。 我测试了我的其他对撞机,发现只有子弹平台对撞机才能做到这一点, 而玩家平台、敌人平台、子弹玩家和子弹敌人对撞机则没有。 此外,我的摧毁子弹的功能,当它们击中平台时,奇怪地工作正常。 我的子弹只有在撞到平台时才会被摧毁。

有人知道如何解决这个问题吗?

编辑: 我为我的游戏创建了一个 Github 存储库以获得更好的洞察力。 (至少我希望我这样做了,我以前没有使用过 Github。) https://github.com/Kiroho/Game

编辑2: 四处测试后,我发现这一定与我如何使用 ammogroup 类有关。

我发现,如果我通过 ammogroup(通过 createMultiple)创建子弹,碰撞器会开始为每颗子弹射击,直到我至少射击它们一次。 所有现有子弹发射一次后,一切正常,对撞机按预期发射 -> 仅在碰撞时发射。

我的子弹平台对撞机

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

弹药组。我将它们用作每种武器的“杂志”

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

}

子弹类

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

fire(x,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);
    }
}

}

附加: 玩家(和敌人)有一个武器对象。 这个武器对象有一个 ammogroup 对象,并设置它的射弹、射击时的速度、将其分配给组等。

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,direction) {
    this.ammogroup.fire(x,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)
}

}

解决方法

我发现创建时必须禁用子弹对象的主体,否则对撞机会触发。我不知道为什么,但这似乎是(或多或少)已知的事情。虽然 body.checkCollision.none=true 有效,但使用 body.enable = false 禁用身体的物理可能是更好的解决方案,因为它禁用所有物理,而不仅仅是碰撞。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。