How do you load a tmx/xml file in javafx?

2020-05-03 21:09发布

问题:

What is the most efficient way to add a tile map in a JavaFX program?

The program that I use for creating tile maps is called "Tiled", and the saved files are in .tmx format.

Now, the question is, how do I import that exactly in my 2d game? I haven't been able to find helpful information on the net.

Code examples are greatly appreciated.

Thanks

回答1:

Use the Eppleton JavaFX TileEngine (unfortunately this link is now dead: similar content is still hosted on javageeks: Beware link comes with annoying add popup).

Update: Unfortunately the eppleton blog entry previously linked, which contained detailed information on creating a tile engine using JavaFX, no longer exists.

I have no idea how to import the file, still.

You will not find at present some library somebody has created and packaged as a binary jar you can just add to your class path to get a generic tile reader for JavaFX.

Loading a TileMap is a complex task. The Eppleton link I provided provides detailed information on how to implement that task as well as source code snippets. The Eppleton information is the best available on TileMaps in JavaFX. By using the Eppleton engine you can reduce the effort involved to load a TileMap to:

TileMap map = TileMapReader.readMapFromFile(fileURL);

There is a github repository for the Eppleton game engine. I tried it and most of the code is there but some of the dependencies for the project did not resolve for me so I was unable to build it. You can contact the author for assistance in resolving dependencies or you can fork the engine and patch it to allow it to work for you. The approach the engine seems to take is to use an abstraction for canvas so that the engine can render into different canvas types (JavaFX canvas being one of these). But I couldn't find the code for the abstraction layer.

The core code for the time map reader is in the de.eppleton.fx2d.tileengine package. You can fork the code and study it to understand how in detail how the tile map reader works and how you might adapt it for your game.