0
我試圖做一個記憶遊戲在Android上,但我不能弄清楚,用相同的圖像匹配2個按鈕的代碼..Android的記憶遊戲
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.TextView;
public class Memory extends Activity {
TextView tv;
int count = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.one);
btn1.setBackgroundResource(R.drawable.background);
Button btn2 = (Button) findViewById(R.id.two);
btn2.setBackgroundResource(R.drawable.background);
btn1.setOnTouchListener(btn1OnTouchListener);
btn2.setOnTouchListener(btn2OnTouchListener);
String match1 = "btn1=btn2";
String ans1a = match1.substring(0, match1.indexOf("="));
String ans1b = match1.substring(match1.indexOf("=") + 1, match1.length());
}
OnTouchListener btn1OnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.puppy2);
if (count < 2)
count = count + 1;
//matching code?
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
while (count==2) {
count = 0;}
//matching code?
}
return false;
}
};
OnTouchListener btn2OnTouchListener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.setBackgroundResource(R.drawable.puppy8);
if (count < 2)
count = count + 1;
}
else if (event.getAction() == MotionEvent.ACTION_UP) {
}
return false;
}
};
你可以給Button一個標識符來比較兩個圖像是否相同?爲他們分配一個號碼等? – 2009-12-15 07:04:54