有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

我正在为一个高中项目创建一个神奇宝贝游戏,很难让数组中的每个对象都受到这种方法的影响。我希望所有火系角色都能承受水系攻击的双倍伤害

问题是attack方法只在一个character上运行

我发现数组中只有一个对象会受到影响,但我希望它对我制作的每个对象和角色都是通用的

我的阵列


    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) 个答案