2009-12-15 5 views
0

je tentais de faire un jeu de mémoire sur Android, mais je ne peux pas comprendre le code qui correspondant à 2 boutons avec la même image ..Android jeu de mémoire

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

Pourriez-vous indiquer à Boutons un identifiant à comparer si les deux images sont identiques? Attribuez-leur un numéro, etc? –

Répondre

0

Je suis venu ici à la recherche pour commencer sur les portails web pour Android.

Mais je l'ai fait écrire jeu de mémoire pour mon projet de classe, je SRCs en correspondance, et commutées classes CSS "visible" ou "invisible ".

Vous pouvez voir tout le code ici -

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

Je cherche toujours été juste pour commencer avec Android, où serait un bon endroit/livre.

Questions connexes