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
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 = ...;
如果要在3个对战游戏中实现互换,则应在两个角色上同时使用move Action。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.