0

我正在使用按钮创建一个简单的打鼹鼠游戏,到目前为止,我仅在单击按钮进入计数时才设法获得游戏分数。未按下按钮时如何注册未命中计数?

b1 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);
b2 = (Button) findViewById(R.id.button1);
    b1.setOnClickListener(this);

b1 = (Button)findViewById(R.id.button1);
b2 = (Button)findViewById(R.id.button2);
final Handler handler = new Handler();
        Runnable runnable = new Runnable() { public void run() {

                int x1= r1.nextInt(array.length);
                int x2= r2.nextInt(array.length);

                b1.setText(array[x1].toString()); 
                b1.setText(array[x1].toString()); 

                int rando = (int)((Math.random()) * 2000);  


                handler.postDelayed(this, rando);  //for interval...
            }
        };

handler.postDelayed(runnable, 2000);
public void onClick(View v) {
    // TODO Auto-generated method stub
        if (v.equals(b1)){
            //Toast.makeText(Random_textviewActivity.this, "Hello", Toast.LENGTH_SHORT).show();
            count++;
            score.setText(String.valueOf(count));
            b1.setText("");
        }
        else if (v.equals(b2)) {
            count++;
            score.setText(String.valueOf(count));
            b2.setText("");             
        }

这些是我的代码的一部分,有没有办法注册一个未点击按钮的计数?

更新


好的,我现在已经掌握了整个想法,但是由于我还不太擅长 android 编程,所以在尝试做你认为我的事情时,我遇到了很多错误。以下是部分到目前为止我得到的代码:

b1 = (whackamolebutton) findViewById(R.id.button1);
 ((whackamolebutton) b1).setmoleactive(this);

public class whackamolebutton extends Button{

public whackamolebutton(Context context) {
    super(context);
    // TODO Auto-generated constructor stub
}//it insist I must cast a method here which I'm not sure why
 //from this point onwards I got confused by the auto fix from eclipse.
public void setmoleactive(Random_textviewActivity random_textviewActivity){
    if (boolean active){
                      count++
                                    }
                else{
                     misscount++              
                    }
}
public int getScore(){
    return count;

}

任何有关代码的帮助将不胜感激。在此先感谢您。

4

1 回答 1

0

我更喜欢实现自定义按钮:

public class WhackAMoleButton extends Button {
  public void setMoleActive(boolean active) {
  ...
  }
  public int getScore() {
  ...
  }
}

这个想法是从所有可见的“WhackAMoleButton”中收集总分/计数,并将计数逻辑移动到 WhackAMoleButton-Class 中。如果按钮从moleActive(true) 变为moleActive(false) 并且在此期间没有点击,您可以提高“未点击”计数。

于 2013-10-14T11:57:33.893 回答