Loading maps that use more tilesets in Java


(Andrea Betti) #1

Hi, I’m working on a project in Java where I have to load a map file using the lib-tiled library and JavaFX.
If the map uses just a tileset (a .tsx file) it displays correctly, but if I load a map that uses tiles from more .tsx files, the program swaps the tiles from the first tilesets with random tiles from the last tileset used. Internally, the .tmx file seems to be saved correctly.

This is my code, thanks for the help.

public class Main extends Application {

    public static void main(final String[] args) {
        launch(args);
    }

    @Override
    public void start(final Stage primaryStage) {
        primaryStage.setTitle("Hello World!");

        final GridPane grid = new GridPane();
        grid.setBorder(Border.EMPTY);
        grid.setAlignment(Pos.CENTER);

        final Scene scene = new Scene(grid, Color.BLACK);

        primaryStage.setScene(scene);
        primaryStage.show();

        try {
            final Map map = new TMXMapReader().readMap("bin\\orthogonal-outside.tmx");

            final int width = map.getWidth();
            final int height = map.getHeight();
            // final int tileHeight = map.getTileHeight();
            // final int tileWidth = map.getTileWidth();

            map.forEach(layer -> {
                if (layer instanceof TileLayer) {
                    final TileLayer tileLayer = (TileLayer) layer;

                    for (int x = 0; x < width; x++) {
                        for (int y = 0; y < height; y++) {
                            final Tile tile = tileLayer.getTileAt(x, y);
                            if (tile != null) {
                                final Image tileImage = SwingFXUtils.toFXImage(tile.getImage(), null);

                                final ImageView iv = new ImageView(tileImage);
                                grid.add(iv, x, y);
                            }

                        }

                    }
                } else if (layer instanceof ObjectGroup) {
                    final ObjectGroup objGroup = (ObjectGroup) layer;
                    objGroup.forEach(obj -> {
                        final Tile tile = obj.getTile();
                        if (tile != null) {
                            final Image tileImage = SwingFXUtils.toFXImage(tile.getImage(), null);

                            final ImageView iv = new ImageView(tileImage);
                            iv.setTranslateX(obj.getX());
                            iv.setTranslateY(obj.getY());
                            iv.setRotate(obj.getRotation());
                            grid.getChildren().add(iv);
                        }
                    });
                }
            });

        } catch (final Exception e) {
            e.printStackTrace();
        }
    }
}

(Thorbjørn Lindeijer) #2

Hmm, there appears to be a bug in libtiled-java in dealing with multiple tilesets. I’ll look into it.


(Thorbjørn Lindeijer) #3

I’ve pushed the following fix which should address your issue: