我的方法中需要影响数组中的所有对象

时间:2019-05-02 12:02:51

标签: java arrays object methods

我正在为一个高中项目创建一个神奇宝贝游戏,并且很难使我数组中的每个对象都受到此方法的影响。我希望所有着火的人物都受到水攻击的双重伤害。

问题在于attack方法仅在一个character上起作用。

我发现数组中只有1个对象会受到影响,但是我希望它对于我制作的每个对象和字符都是通用的。

我的阵列


    ArrayList<Attacks> attacks;
    ArrayList<Characters> characters;

        attacks = new ArrayList<Attacks>();
        characters = new ArrayList<Characters>();

我的攻击班

//Stats--------------
    private int damage;
    private int accuracy;
    private int critChance;

    //Typ----------------------
     boolean fire;
     boolean water;

    public Attacks(int damage , int accuracy , int critChance , boolean fire , boolean water) {
        this.accuracy = accuracy;
        this.critChance = critChance;
        this.damage = damage;
        this.fire = fire;
        this.water = water;
    }
    public boolean isFire(){
        return fire;
    }
    public int getDamage(){
        return damage;
    }
    public boolean isWater(){
        return water;
    }
}

我的角色课

//Types
    private boolean fire;
    public int health;

    public Characters(int health, boolean fire) {
        this.health = health;
        this.fire = fire;
        }

    public boolean characterIsFire()
    {
        return fire;
    }

    public int getHealth() {
        return health;
    }
}

我的对象的构造函数


        //Construktor for Characters--------------------

        final Characters fredrik = new Characters(1000 , false);
        characters.add(fredrik);
        final Characters jonte = new Characters(1000 , false);
        characters.add(jonte);

        //Construktor for attacker -------------------
        //Fredriks attacks
        final Attacks fireball = new Attacks(1 , 10 , 10 , false , true);
        attacks.add(fireball);
        final Attacks heatwave = new Attacks(1 , 0 , 10 , true , false);
        attacks.add(heatwave);

        //Jontes attacks
        final Attacks waterball = new Attacks(3 , 0, 4, false , true);
        attacks.add(waterball);

我希望所有角色和攻击都受到影响的方法。

    public int attacking(Attacks a) {
        int damage;
        Characters attacked = characters.get(//All the characters in the array);
        if (attacked.characterIsFire() && a.isWater())
        {
            damage = (a.getDamage() * 2);
        }
        else
        {
            damage = a.getDamage();
        }
        return damage;
    }

0 个答案:

没有答案