シンプルなメモリーゲームを作りたいです。16ボタンを使用しました。特定のボタンがクリックされたときに反応する方法は知っていますが、ボタンのクリックごとに反応して、一致するボタンが選択されているかどうかを確認するにはどうすればよいですか (今は必ずしもそうではありません)。
2 に答える
0
ロジックを使ってプログラムを作成できましたが、非常に長いです。ボタンごとに同じことを行う必要があります。
Button B1;
int x,y; //give them values and compare (example: B1=1, B2=2, B3=1 .. B1&B3 the same picture)
int turn = 1; //to know whos turn (x or y), default start on x
int numberOfClicks=0; //when 2 buttons clicked, check
//in the OnCreate()
B1 = (Button) findViewById(R.id.b1); //assume B1's value = 1
B1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) { //------------------------------------ OnClick Starts here
if (turn==1){
//use x
x=1;
turn=2; //flip the turn
numberOfButtons++; //one is clicked so far
}else{
//use y
y=1;
turn=1;
numberOfButtons++;
}
if(numberOfButtons==2){
//check
if(x==y){
//same
numberOfButtons=0; //restart counter
}else{
//not the same
numberOfButtons=0; //restart counter
}
}//end of OnClick
}); //end of button OnClickListener
B2=7 の場合、OnClick の x と y は = 7 である必要があります。各 OnClick は異なる x と y の値を持ちます。
于 2013-05-13T19:13:55.597 に答える