LibGDX having trouble with tiled maps

So I’ve recently began to learn how to use LibGDX and I’m now trying to load a tiled map. I’ve been reading this.

So I have my TMX file located at assets/maps/tilemapxml.tmx along with the .png and .pack files. I’m trying to load it via the AssetManager, using this code:

AssetManager manager = new AssetManager();
manager.setLoader(TiledMap.class, new TmxMapLoader(
		new InternalFileHandleResolver()));
manager.load(mapFile, TiledMap.class);
manager.finishLoading();

map = manager.get(mapFile);

Then I’m trying to render the map how it says to:

new OrthogonalTiledMapRenderer(map, unitScale);

But I get a lot of errors like so;

Couldn't load dependencies of asset: maps/tilemapxml.tmx
Error parsing file: maps/tilemapxml.tmx

Q1: Am I doing anything wrong at the moment??
Q2: I’m confused and I feel like I’m missing a lot of code here. What else do I need to do?

iirc the system of tiled maps that uses .pack is deprecated for libgdx. Try updating libgdx using the setup-ui or downloading the latest nightly, and check out the new maps api here.

But that’s not why you’re getting an error - the old system should still work, it’s just harder to use, more tedious, and not supported. The error probably means you misplaced the .png, so even if it’s in the same folder try opening up the .tmx in notepad or notepad++ and see what the path of the .png is supposed to be (oh and try refreshing the project first before you do anything else).

LibGDX does not support storing your map layer data in XML format. Use csv, base64, base64-gzip, or base64-zlib to store your map layer data. This is probably the cause of the errors.

Using the tiled-preprocessor is still supported with the new api, but you have to use AtlasTmxMapLoader to load the map (and use the latest version of the tiled-preprocessor).

He named it tilemapxml. It’s still a .tmx though.

And one of the formats for tmx files is to store layer data as xml, which is not supported. I made the educated guess that if the file was named tilemapxml.tmx that it used the xml data format.

I decided to take a different approach and load it with TmxMapLoader. I followed this tutorial. However I still get a lot of errors parsing the file. I was previously using XML encoding, so I changed it to base64 and also tried base64 with gzip compression, however I still get a lot of errors. Here’s the stack trace:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error parsing file: maps/tilemap.tmx
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:79)
	at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:116)
	at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
	at en.mash.splooke.maps.Map.loadMap(Map.java:19)
	at en.mash.splooke.screen.GameScreen.show(GameScreen.java:40)
	at com.badlogic.gdx.Game.setScreen(Game.java:62)
	at en.mash.splooke.screen.SplashScreen.render(SplashScreen.java:28)
	at com.badlogic.gdx.Game.render(Game.java:46)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing XML on line 1 near: <?xml version="1.0" encoding=
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:322)
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:60)
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:67)
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:77)
	... 9 more

Also here is my TMX file:

<?xml version="1.0" encoding="utf-8"?>
<map version="1.0" orientation="orthogonal" width="200" height="32" tilewidth="32" tileheight="32">
  <tileset firstgid="1" name="Blocks" tilewidth="32" tileheight="32" spacing="0" margin="0">
    <image source="tiles.png" />
  </tileset>
  <layer name="Platforms" width="200" height="32">
    <properties>
      <property name="@Description" value="" />
    </properties>
    <data encoding="base64">this is mainly just A's, I removed it because it's so long</data>
  </layer>
</map>

This is how I’m loading the map file:

	public Map(String file) {
		this.mapFile = file;
	}

	public void loadMap(float unitScale) {
		map = new TmxMapLoader().load(mapFile);
		renderer = new OrthogonalTiledMapRenderer(map, unitScale);
	}

I checked the tutorial twice over and my code should work. ???

That BOM () at the beginning of your file is the problem. Try resaving the file without the BOM. Tiled doesn’t usually add that, did you edit the file with something else afterward? You should be able to open it in something like Notepadd++ and just save without the BOM.

Thank you. I was using Tide map editor which must have added it. I opened it in notepad previously but didn’t see the BOM but using the eclipse text editor let me remove it.

Hello,

I’m having the same problem, I’m using Tiled and I’ve tried with every possible format (except xml). And also creating new maps but I keep getting the same error while parsing whatever I do:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.SerializationException: Error parsing file: /maps/tres.tmx
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:79)
	at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:116)
	at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:104)
	at com.cipolino.drinkgame.Screens.GameScreen.show(GameScreen.java:43)
	at com.badlogic.gdx.Game.setScreen(Game.java:62)
	at com.cipolino.drinkgame.DrinkingGame.create(DrinkingGame.java:13)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:136)
	at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: \maps\tres.tmx (Internal)
	at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:133)
	at com.badlogic.gdx.utils.XmlReader.parse(XmlReader.java:76)

This is where I load the map:

@Override
	public void show() {
		//Cargamos mapa
		TmxMapLoader loader = new TmxMapLoader();
		map = loader.load("/maps/Jar.tmx");
		
		//pasamos mapa al renderer
		render = new OrthogonalTiledMapRenderer(map);

	}

And this the Tmx File (Jar.tmx):

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" orientation="orthogonal" width="18" height="24" tilewidth="64" tileheight="64">
 <tileset firstgid="1" name="tilesv1" tilewidth="64" tileheight="64" spacing="1" margin="1">
  <tileoffset x="1" y="1"/>
  <image source="tilesv.png" width="196" height="196"/>
 </tileset>
 <layer name="Tile Layer 1" width="18" height="24">
  <data encoding="base64" compression="zlib">
   eJxjZWBgYKUSZgdiRiIxBx5zGEmwE5/a4WAOM5awIjaMGdHsRLefXH+NmjNqzqg5g9McTgZEvmejwBxi7cCHmahkHz4MABxUBzE=
  </data>
 </layer>
</map>

Obviously I have the file tilesv.png in the package assets.

Any Ideas of what could be wrong?

This is a really old thread. ::slight_smile:

Anyway, if you’d have read the previously replies, the problem is that a BOM has been saved in the file. Open the file with a text editor such as eclipse text editor or notepad++ and you should be able to remove the BOM.

Yes, sorry for re-opening the thread, it was exactly the same question so I thought it was the right thing to do.

Thank you for the answer but the code for the Tmx I posted was from Eclipse Text Editor and I’ve also opened it with Notepad++. I even tried with File BOM detector but there is no BOM. That’s why I was asking for other ideas.

That’s odd. There’s obviously an error parsing the file. Have you edited it? All I can suggest is trying to recreate the map.

Oh! just found it… it couldn’t find the file because I put “/” in front of maps in the path to the file…

Thank you for your help :slight_smile: