【问题标题】:How can I make a method from a different class start when I press a button?当我按下按钮时,如何从不同的类开始方法?
【发布时间】:2019-09-16 06:24:31
【问题描述】:

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

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

我正在使用 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();
        }
    }

我希望在我按下按钮时启动攻击方法。这样任何角色的生命值都会因任何攻击而降低,无论是否着火。

【问题讨论】:

    标签: java android class methods


    【解决方案1】:

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

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

    if(fredrikVald)
    {
    //add code here
    spelare.setText("Fredrik Used Fireball");
    }
    

    和水球部分类似的代码

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 1970-01-01
      • 1970-01-01
      • 2015-02-10
      • 1970-01-01
      相关资源
      最近更新 更多