2009-12-15 105 views
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; 

    } 
}; 
+0

你可以給Button一個標識符來比較兩個圖像是否相同?爲他們分配一個號碼等? – 2009-12-15 07:04:54

回答

0

我來到這裏尋找上手在Android的門戶網站上。

但我的確寫了記憶遊戲我類項目,我匹配索馬里紅新月會,以及切換CSS類「可見」或「隱形」。

你可以看到這裏所有的代碼 -

http://ciswebs.smc.edu/cs85/janckila_alexander_a/memorygameproject.html

我現在還在找上了先手與Android,這裏將是一個不錯的地方/書開始。