0

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;
    }
EM10
  • 795
  • 7
  • 12
  • 24
  • what do you mean with override each other? are they overlapped for a short time? in which order do you process your tiles? are you doing this sequentially or or concurrently? – Harmlezz Mar 14 '14 at 11:51
  • Yes they overlap each other. I process my tiles sequentially. – EM10 Mar 14 '14 at 12:12
  • You are incrementing tileX inside the if. Shouldn't you rather be resetting it? – burntsugar Mar 14 '14 at 12:26
  • I increment tileX so the tile moves to position tileX=840 so I can see the tile move again from right to left. – EM10 Mar 14 '14 at 12:44

2 Answers2

2

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.

Jack
  • 508
  • 1
  • 9
  • 18
  • I have found a problem. The tiles moves at a speed of speedX=-5 when I jump and when I walk on the tiles speedX is -3. If speed of speedX is the same when I jump and when I walk my tiles are correct. I don't understand why that gives me a problem. – EM10 Mar 14 '14 at 12:39
  • So when you move a tile, the speedX is -5 when it's supposed to be -3? Think about where in your program speedX is being assigned or changed. I would recommend posting more code if you need further help. – Jack Mar 14 '14 at 12:45
1

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);
}
Harmlezz
  • 7,972
  • 27
  • 35
  • My problem was that the speed of tiles changed depending if I jumped or not. Now I have set the speed of the tiles the same. Now I don't get the problem. – EM10 Mar 14 '14 at 13:02