1

I want to develop an simple memory game on Android.

The memory game have squares like the picture below. How should I determine which of the square is pressed? Should I use image buttons? Personally I don't think it is an good idea to make the game with image buttons. Could you suggest me an solution on how I should determine which of the square is pressed?

enter image description here

EM10
  • 795
  • 7
  • 12
  • 24
  • 1
    I'd make each square is a Button or an ImageView or a TextView or ... How else woud you do, if not? – Phantômaxx Apr 11 '14 at 09:40
  • I want to use the Graphics class to draw the image on the screen. What I know is where on the screen an image is placed and where the user clicks. – EM10 Apr 11 '14 at 09:44
  • create a table with each square a cell. Render the cell using renderer and add listener to the table which will give you the row and column in the table... – Yogesh Patil Apr 11 '14 at 09:46

3 Answers3

1

I think ImageViews for each square is the best way to go, no need for buttons.

Note: Views would be sufficient, you can implement onClick(View) for any view.

UPDATE: Since you have a Graphics (as opposed to Views) for your squares, you may indeed use the coordinates to determine which square is touched, but I don't think this is the easiest, nor the best approach.

Why don't you want a GridLayout or a TableLayout containing ImageViews?

Joffrey
  • 32,348
  • 6
  • 68
  • 100
  • Because I want to use animation also when a certain square is pressed, Is it possible to have animation in gridview? I want to make a game with good graphics, I am not sure if gridview is a good solution. – EM10 Apr 11 '14 at 09:54
  • I've never used it myself, but you can take a look at http://developer.android.com/reference/android/view/animation/GridLayoutAnimationController.html – Joffrey Apr 11 '14 at 09:57
  • Is it better to use Views or GridView to make the game work on all screen sizes? – EM10 Apr 11 '14 at 10:18
  • I guess yes, cleaner and easier to implement than using Graphics explicitly. – Joffrey Apr 11 '14 at 12:28
1

If you have a view where you draw the squares then put a onClick() listener in that view and inside the onClick() do like this: (this is taken from a Sudoku game I made)

@Override
public boolean onTouch(View v, MotionEvent event)
{
    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            // keep where the user first touch the screen
            touchedX = (int) event.getX() / wid33; // the square width
            touchedY = (int) event.getY() / hei33; // the square height
            break;

        case MotionEvent.ACTION_UP:
            // see if the touched square is the same
            int X = (int) event.getX() / wid33;
            int Y = (int) event.getY() / hei33;
            if (X == touchedX && Y == touchedY)
            {
             // the square is the same, do what you have to do
            }
            break;

        case MotionEvent.ACTION_CANCEL:
            touchedX = -1;
            touchedY = -1;
            break;

        default:
            return super.onTouchEvent(event);
    }
    return true;
}
ja_mesa
  • 1,971
  • 1
  • 11
  • 7
0

Consider using Canvas or even OpenGL ES to render your game. This approach gives you more flexibility and better performance.

To check witch square was touched handle touch event you can use OnTouchListener.

This or this answer might be helpful to start your code.

Community
  • 1
  • 1
LR89
  • 397
  • 1
  • 4
  • 14