tags:

views:

193

answers:

1

I was trying to do a memory game on android, but i can't figure out the code that matching 2 button with the same image..

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;

 }
};
A: 

I came here looking to get started on web portals for Android.

But I did write memory game for my class project, I matched srcs, and switched css classes "visible" or "invisible".

You can see all of the code here -

http://ciswebs.smc.edu/cs85/janckila%5Falexander%5Fa/memorygameproject.html

I am still looking on were to just get started with Android, where would be a good place/book.

Alex