按下按钮时,如何从其他类开始制作方法?

时间:2019-04-26 17:47:29

标签: java android class methods

我创建了一种方法,该方法应该对着火的人造成“双重伤害”,并在按下按钮时使用此方法。

这是我的高中项目,它是一个类似于神奇宝贝的简单游戏,但我有一些问题。

我正在使用Android Studio。

攻击的构造函数

public Attacks(int damage , int accuracy , int critChance , boolean water , boolean fire) {
        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 Characters(int health, boolean fire) {
        this.health = health;
        this.fire = fire;
        }

    public boolean characterIsFire()
    {
        return fire;
    }

    public int getHealth() {
        return health;
    }

当我按下按钮时会发生什么:

这是另一个类。这是在“ Fight View”类中,这是布局与代码匹配的地方。


attackOne.setOnClickListener(new View.OnClickListener() {

@Override

 public void onClick(View v)
 {
if(fredrikVald)
{

spelare.setText("Fredrik Used Fireball");
 }

 if(jonteVald)
{

spelare.setText("Jonte Used Waterball");
                }
            }
        }); 

攻击方法。

这是在“攻击”类别中。

 public void attack(Attacks a)
    {
        Characters attacked = characters.get((player_in_turn + 1) % NR_OF_CHARACTERS);
        if(attacked.characterIsFire()&& !a.isFire())
        {
            attacked.health = attacked.getHealth() - (a.getDamage() * 2);
        }
        else
        {
            attacked.health = attacked.health - a.getDamage();
        }
    }

我希望在按下按钮时启动攻击方法。这样,即使发动或不发动,任何攻击都会降低任何角色的生命值。

1 个答案:

答案 0 :(得分:0)

您需要您的Attack类的实例,假设我们将其称为“ att”。然后,您可以添加

<? for (var i = 0; i < data.length; i++) { ?>
  <li><?= data[i] ?></li><br>
<? } ?>

Attacks fireball = new Attacks(/*whatever values you want*/);
att.attack(fireball);

以及与水球部分类似的代码