2009-12-15 78 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,这里将是一个不错的地方/书开始。