Question

I have a 800x480 screen size where I play the game. I have tiles of size 60x120 which I place on the screen in a position. From beginning I have created 8 tiles see picture below. The tiles are always moving from right to left. When the tile is out of screen I recreate the tile in a new position. The problem I have is that the tiles are not deterministic, some of the tiles overlap each other when moved to new position.

enter image description here

The code below checks the tiles position. If the tile is out of the screen the tile is moved to the new position. The problem I have right now is that the tiles sometimes overlap each other. How can I solve the problem?

 tileX += speedX; // speedX is the speed of tiles moving to the left.
    if(tileX <= -60){ // Checks if tile position is out of screen
       tileX += 840;
    }
Was it helpful?

Solution 2

Due to the lack of information I have to guess how your algorithm, moving the tiles, work. But I suggest (as a working copy for the discussion) an algorithm I would come up with. The important thing is that I do move all tiles to the left in a separate loop. Otherwise if you move a Tile and if it is to far to the left remove it from the List and add it to the end of the List, you would process it twice. Is this possble the case in your code?

All tiles are sorted in the List from most left to most right. The algorithm keep them this way. The loops processes the most left one first to avoid overlapping (even so they are not drawn yet). As mentioned above, it is a guess due to the lack of information.

public interface Tile {
    public void moveLeft(int pixel);
    public void moveRight(int pixel);
    public int getX();
}

public static void main(String[] args) {
    int speedX = 20;
    List<Tile> tiles = getMovingTiles();
    for (Tile tile : tiles) tile.moveLeft(speedX);
    while (tiles.get(0).getX() <= -60) {
        Tile tile = tiles.remove(0);
        tile.moveRight(840);
        tiles.add(tile);
    }
}

private static List<Tile> getMovingTiles() {
    throw new UnsupportedOperationException("not yet implemented");
}

== UPDATE ==

A more efficient algorithm blueprint:

public interface Tile {
    public void moveLeft(int pixel);
    public int getX();
    public void setX(int pixel);
}

public static void moveTiles(List<Tile> tiles, int speed, int offScreen, int startPos) {
    tiles.add(null);
    for (int idx = 0; tiles.get(idx) != null;) {
        Tile tile = tiles.get(idx);
        tile.moveLeft(speed);
        if (tile.getX() <= offScreen) {
            tile.setX(startPos);
            tiles.add(tiles.remove(idx));
        } else idx++;
    }
    tiles.remove(null);
}

OTHER TIPS

Instead of just adding an arbitrary value to the location of tileX, I would find a number that you could set it to so that it is moved consistently.

if(tileX <= -60){
   tileX = 780;
}

for example.

If this doesn't work, you might have to post more code from your game loop to find the problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top