本文介绍了执行的操作时,单击按钮n次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void boss(final Boss boss) {
    forward.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            boss.setHp(boss.getHp() - 1);
            log("" + boss.getHp());
        }
    });
    if(boss.getHp() > 0){
        boss(boss);
    }
}

因此​​,在上述code,我想有bossHp减少1正向每一次点击。有一个计算器错误与code。 (这只是调用的老板()的无限循环。)我怎样才能把它保持1每当用户点击向前时间减少bossHp,然后停在bossHp< = 0

So, in the above code, I am trying to have bossHp reduce by 1 every time forward is clicked. There is a stackoverflow error with the code. (It is just calling boss() in an infinite loop.) How can I have it keep reducing bossHp by 1 every time the user clicks forward, and then stop when bossHp <= 0?

注:按钮向前别处使用,也

Note: Button forward is used elsewhere, too.

谢谢!

编辑:很抱歉,错字前面!显示的code现在是正确的code。有任何想法吗?谢谢!

澄清:

我想有发生,为它检查bossHp每一次前进按钮被点击UNTIL bossHp&LT; = 0。在这一点上,我想退出的方法,但在此之前没有的话

What I want to have happen, is it checks bossHp every time Button forward is clicked UNTIL bossHp <= 0. At which point I want to exit the method, but not before then

推荐答案

这应该工作。你也不必经过老板的功能参数。你可以把对象作为外地在你的类。

This should work. Also you don't have to pass boss as function parameter. You can keep that object as field in your class.

public void boss(final Boss boss) {
    forward.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if(boss.getHp() > 0){
                boss.setHp(boss.getHp() - 1);
                log("" + boss.getHp());
            } else {
                //boss is dead
            }
        }
    });
}

这篇关于执行的操作时,单击按钮n次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-02 12:45