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 に答える