 |
|
เราทำเกมจับคู่รูปภาพน่ะค่ะ ต้องการให้จับคู่แล้วแสดง Alert Dialog เป็นรายละเอียดของรูปๆนั้น
โดยรายละเอียดของแต่รูปแต่ละคู่ที่จับได้ จะไม่เหมือนกัน ซึ่งเราต้องเช็คว่ารูปที่จับคู่นั้นเป็นรูปอะไร แล้วแสดงรายละเอียดให้ตรง
ซึ่งภาพเราประกาศเป็น Array
Code (Android-Java)
private Integer[] mThumbIds = { R.drawable.one, R.drawable.one,
R.drawable.two, R.drawable.two , R.drawable.three,
R.drawable.three, R.drawable.four, R.drawable.four,
R.drawable.five , R.drawable.five , R.drawable.six,
R.drawable.six , R.drawable.seven , R.drawable.seven,
R.drawable.eight , R.drawable.eight , R.drawable.nine,
R.drawable.nine , R.drawable.ten , R.drawable.ten };
int opened = 0;
int firstClick, secondClick;
แล้วก็มีการ random ภาพจาก Array แล้วทำการสลับที่ภาพ
Code (Android-Java)
private void shuffleArray(Integer[] mThumbIds2) {
// TODO Auto-generated method stub
Random rnd = new Random();
for (int i = mThumbIds2.length - 1; i >= 0; i--) {
int index = rnd.nextInt(i + 1);
// Simple swap
int a = mThumbIds2[index];
mThumbIds2[index] = mThumbIds2[i];
mThumbIds2[i] = a;
}
}
มีการเช็คว่าภาพแรกที่เปิด ตรงเป็นภาพที่สองที่เปิดไหม???? ถ้าตรงก้อให้ภาพหายไป removeImage ถ้าไม่ตรงก้อพลิกกลับ cardBack
Code (Android-Java)
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
imageView = (ImageView) v;
imageView.setAdjustViewBounds(true);
opened++;
final Handler handler = new Handler();
if (opened == 1) {
firstClick = position;
firstView = (ImageView) v;
imageView.setImageResource(mThumbIds[position]);
} else if (opened == 2) {
secondClick = position;
if( firstClick != position )
{
imageView.setImageResource(mThumbIds[position]);
imageView.setClickable(false);
if (mThumbIds[firstClick].compareTo(mThumbIds[secondClick])== 0) {
final AlertDialog.Builder adb = new AlertDialog.Builder(this);
if (................เป็นรูปแอปเปิ้ล......................) {
AlertDialog ad = adb.create();
ad.setMessage("Message");
ad.show();
} else if (................เป็นรูปฝรั่ง......................) {
AlertDialog ad = adb.create();
ad.setMessage("Message 2");
ad.show();
handler.postDelayed(removeImage, 800);
} else {
handler.postDelayed(cardBack, 600);
}
}
} else
{
handler.postDelayed(cardBack, 600);
}
}
ส่วนตรงที่เราทำไม่ได้คือ if(............................) ไม่รุ้ว่าจะเอาภาพมาเช็คยังไง เพราะค่าที่เก็บในตัวแปรมีแต่ค่าของต่ำ
แหน่ง position ช่วยดูให้หน่อยนะค่ะ
Tag : Mobile, Android
|
|
 |
 |
 |
 |
Date :
2012-11-17 01:05:26 |
By :
formicz |
View :
3247 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |