2

So I am trying to create a simple match 3 game. I created an actor class Tile to store the image and data about row and column and created another class GameField that has an array of size 9 * 9 of tile.

I created an function named swap tiles to swap the tiles which is as follow but it has an strange behaviour that after any two tiles have been swapped one of them responds to clicks whereas the other one stops responding to the clicks further.

Here are the input listener of Tile class and the swap tile function of gamefield:

addListener(new InputListener() {

        @Override
        public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
            setActiveSprite(row, column, colour);
            System.out.println("clicked" + "row :" + row + "column" + column);
            return false;
        }

    });

public void setActiveSprite(int r, int c, int colour){
    System.out.print(colour);
    if (!(gf.activeTile.colour==0)){
        gf.swapTiles(r, c, colour);
        gf.activeTile.colour=0;
    } else {
        gf.activeTile.setAll(r, c, colour);
    }
}

public void swapTiles(int r, int c, int colour) {


    gameField[activeTile.row][activeTile.column].addAction(Actions.moveTo(
            (40 + ((gameField[r][c].getHeight() * c) + (10 * c))),
            (200 + (gameField[r][c].getWidth() * r)),
            0.5f));


    //setting the new position of clicked tile
    gameField[r][c].addAction((Actions.moveTo(
            (40 + ((gameField[r][c].getHeight() * activeTile.column) + (10 * activeTile.column))),
            (200 + (gameField[r][c].getWidth() * activeTile.row)),
            0.5f)));


    Tile Temp = gameField[activeTile.row][activeTile.column];


    gameField[activeTile.row][activeTile.column] = gameField[r][c];
    gameField[activeTile.row][activeTile.column].row = activeTile.row;
    gameField[activeTile.row][activeTile.column].column = activeTile.column;


    gameField[r][c] = Temp;
    gameField[r][c].row = r;
    gameField[r][c].column = c;


}

Please notify me the possible error which i think is there in the swapTile function

2 Answers2

2

This way you do not swapping objects - you are just assigning references. Inside method variables are not being copied by value in Java - then after

     Tile Temp = gameField[activeTile.row][activeTile.column];

You have same instances both in gameField[activeTile.row][activeTile.column] and Temp so when you are changing one of them like here:

     gameField[activeTile.row][activeTile.column] = gameField[r][c];

the second one is being impacted also what causes issues.

What you need to do is to copy object step-by-step what you can achieve by implement copying constructor. Take a look at this thread. In a nutshell it would look like

    class Tile extends Actor {
        int row, column; //and others

        public Tile() {}

        public Tile(Tile tile) {
            this();

            //now copying another tile's values
            this.row = tile.row;
            this.column = tile.column;
        }
    }

And the swap should be implement not by assigning references but creating new object like

    Tile temp = new Tile( gameField[activeTile.row][activeTile.column] );

Also please notice that good practise is to name variables starting with lower case and classes with upper case. Then your temp variable definition should looks like

Tiled temp = ...;
Community
  • 1
  • 1
m.antkowicz
  • 13,268
  • 18
  • 37
  • constructor can be used if the contents of classes are known what about if the contents are not known like Texture class as my Actor extends an Image and contains an texture class too. – Shivanshu Kant Prasad Apr 04 '16 at 10:22
  • 1
    you can use Image's `Image(Drawable drawable)` constructor and pass the `getDrawable()` other Tile's result to this – m.antkowicz Apr 04 '16 at 11:37
1

if you want to achieve swap in 3 match game.you should use move Action on both actors with same time.