I have a 4x4 grid of 60 pixel wide cells. My issue is if I have more than one instance of this class (Tile) on the grid and I move them around it works just as to be expected for most of it but if there are two Tiles horizontally aligned and I chose either left or right, they flip flop. It works just fine if I chose up or down. For give me for the broken thoughts for I am about to pass out.
private int direction 0 = left, 1 = right, 2 = up, 3 = down
any help or suggestion is appreciated.
public int getNumOfEmptyCellsInFront(){
System.out.println("getNum() direction = " + direction);
int count = 0;
switch (direction){
case 0:
for (int i = getX() - 1; i > -1; i--){
if ( getWorld().getObjectsAt(i, getY(), Tile.class).isEmpty()){
count += 1;
}
}
return count;
case 1:
for (int i = getX() + 1; i < 4; i++){
if ( getWorld().getObjectsAt(i, getY(), Tile.class).isEmpty()){
count += 1;
}
}
return count;
case 2:
for (int i = getY() - 1; i > -1; i--){
if ( getWorld().getObjectsAt(getX(), i, Tile.class).isEmpty()){
count += 1;
}
}
return count;
case 3:
for (int i = getY() + 1; i < 4; i++){
if ( getWorld().getObjectsAt(getX(), i, Tile.class).isEmpty()){
count += 1;
}
}
return count;
default: break;
}
return 0;
}
public void slideTile(){
switch (direction){
case 0:
for (int x = getX() - 1; x > (getX() - 1) - getNumOfEmptyCellsInFront(); x--){
setLocation(x, getY());
if (getNumOfEmptyCellsInFront() > 1){
}
}
direction = -1;
break;
case 1:
for (int x = getX() + 1; x < (getX() + 1) + getNumOfEmptyCellsInFront(); x++){
setLocation(x, getY());
if (getNumOfEmptyCellsInFront() > 1){
}
}
direction = -1;
break;
case 2:
for (int y = getY() - 1; y > (getY() - 1) - getNumOfEmptyCellsInFront(); y--){
setLocation(getX(), y);
if (getNumOfEmptyCellsInFront() > 1){
}
}
direction = -1;
break;
case 3:
for (int y = getY() + 1; y < (getY() + 1) + getNumOfEmptyCellsInFront(); y++){
setLocation(getX(), y);
if (getNumOfEmptyCellsInFront() > 1){
}
}
direction = -1;
break;
default:
direction = -1;
break;
}
}
