tags:

views:

52

answers:

3

I have 20 cards of which there are 10 pair of images. The player is meant to find a match of each card/image. The problem is that if a player taps or clicks each card twice then that card gets decremented from the remaining ones . I need to disable the click listener of the ImageView. How can do that?

ivOne = (ImageView)findViewById(R.id.ivOne);
ivOne.setId(a[0]);
//final ImageView ivOne = (ImageView)findViewById(R.id.ivOne);
//ivOne.setEnabled(false);
ivOne.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                ivOne.setBackgroundResource(images[a[0]]);
                Log.e("r[0]:", Integer.toString(a[0]));
                if (firstTap)
                {
                    firstId = v.getId();
                    firstTap = false;

                }
                else
                {
                     //ivOne.setEnabled(false);
                     int secondId = v.getId();
                     Log.e("secondId", Integer.toString(secondId));
                     if ((secondId == firstId) && (score != 0)) 
                     {   
                        //ivOne.setEnabled(false);
                       if (ivOneScored == false)    
                       {
                         score--;
                         ivOneScored = true;
                       }
                     }   

                     firstTap = true;
                }


                tvScore.setText("Remaining:" + Integer.toString(score));

            }

    });
A: 

If I understand you right, you just need to say view.setOnClickListener(null) if those two match? If you want to disable both of them, you just store a map to link an ID to a View, and then clear the click listener for both "view" and whichever view belongs to the second ID.

EboMike
+1  A: 

You have to make sure that those cards have the same symbol, but are not the same card.

So your line

if ((secondId == firstId) && (score != 0)) 

must rather look like something in the lines of

if ((firstCard != secondCard) && (secondId == firstId) && (score != 0)) 

where firstCard and secondCard identify the card on the table (not the symbol on the card)

ammoQ
Should i make my own class for cards?Is that what you mean?
Maxood
Seems like a good idea to me.
ammoQ
+1  A: 

I have a memory matching game on the market that works very similar. What I did was make a class that represents the card (with image and id properties) and assign an id (0-9) to the cards and the image as I loaded them into a temporary arraylist of just the 10 image objects.

public class GameTile {
    public int id;
    public Drawable image;
}

Then I iterated over the list twice and added the cards to the main arraylist that I use for my adapter... after which I used Collections.shuffle to shuffle the cards... Now... on to the selection. AS you know, in a memory game, you only want 2 cards flipped at any time. I am using a gridview to hold my cards... so what I did was use the OnItemClickListener from the gridview and not the imageview. Secondly, when a card is clicked, I add the position to another arraylist called "selected" that never contains more than 2 items... the position of the items we are attempting to match... In the OnItemClickListener, when the event is fired, I check to see if the item already exists in "selected" and return if it does... in effect ignoring the click.

if (selected.contains(position)) {
    return;
}

When "selected" contains 2 items, I ignore all clicks until the handler finishes checking for a match.

if (selected.size() > 1) {
    return;
}

So when a user has selected 2 items, I set a handler to call a runnable that checks for a match. If a match is made (by comparing the id fields I set when I first loaded the images), I add the two positions to another arraylist that contains only matched items and that handler also clears "selected" and, if there were matches, I change the images to blanks. When all 20 cards have been matched I fire a win which does all of the win stuff and resets the gameboard.

androidworkz