How can I determine which square in my memory game was pressed?

| | August 11, 2015

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

One Response to “How can I determine which square in my memory game was pressed?”

  1. Here is an idea:

    you probably know of the dimensions of the screen, you know of the resolution of the squares, and at any time when a point on the screen is touched, you know this points x and y coordinates.

    int xSquare = floor(x/squareResolution)
    int ySquare = floor(y/squareResolution)
    

    If you store all your squares in a one dimensional array, the square you want is

    squares = new Square[gridResolution*gridResolution];
    theSquareYouWant = squares[ySquare *gridResolution + xSquare]
    

    If you store it in a 2d array you want square

    squares = new Square[gridResolution][gridResolution];
    theSquareYouWant = squares[xSquare][ySquare]
    

    You should not use image buttons. If you want to animate a square being pressed, create an animated sprite or whatever at the space occupied by the square and then remove it once the animation has finished.

Leave a Reply