ben berizovsky
ben berizovsky

Reputation: 761

LibGDX random space between tile cells

I have created a board of cells that looks like this:

image

As you can see, every few height cells you can see there is more spacing by few pixels.

But this only happens after I drag the map by using camera translation.

Thats how I draw the map:

    batch.setProjectionMatrix(camera.combined);
    camera.update();

    batch.begin();

    batch.draw(sea, -5000, -5000, 0, 0, 10000, 10000);
    int[][] map = context.getBattle().getMap();

    int widthOffset = 0;
    int heightOffset = 0;

    for(int i = 0; i < map.length; i++) {
        for (int j = 0; j < map[i].length; j++) {
            heightOffset += 65;
            batch.draw(tiles.get(map[i][j]), i + widthOffset, j + heightOffset, 65, 65);

        }
        widthOffset += 65;
        heightOffset = 0;
    }

    batch.end();

And my translation is being called here:

public void handleDrag(float screenX, float screenY) {
    camera.translate(-screenX, screenY);
}

by my input procesor:

@Override
public boolean touchDragged(int screenX, int screenY, int pointer) {
    float x = Gdx.input.getDeltaX();
    float y = Gdx.input.getDeltaY();
    context.getRendering().getSeaBattle().handleDrag(x, y);
    return false;
}

I have tried changing the offsetHeight increment but no luck, it starts doing these spaces after my first drag.

Upvotes: 0

Views: 162

Answers (1)

Lucas B
Lucas B

Reputation: 348

Maybe you could create a tiled map instead of drawing separate tiles? Considering that libgdx has support for this so you don't have to do these calculation yourself.

Learn to use this or any other tile map creator:
http://www.mapeditor.org/

Libgdx implementation:
https://m.youtube.com/watch?v=qik60F5I6J4

And or maybe:
https://m.youtube.com/watch?v=zckxJn751Gw

Upvotes: 1

Related Questions