Search code examples
javaandroidlibgdxtiledtmx

LibGDX importing TMX files


Ive been learning to code games in java and libgdx. My most hated part of any language Ive attempted is the Level Editor (.TMX , tiled) parsing. The Github for libgdx says there is a helpful class made for this which Ive been trying to follow the instructions but Im stuck.

Basically if I comment out the following line of code, the app runs fine but of course without any level elements included. When I have this line in code it crashes my phone instantly whilst loading app:

TiledMap map

public Level(OrthographicCamera camera) {
    map = new TmxMapLoader().load("level1.tmx"); /// THIS LINE CRASHES IT
}

I have checked the file 'level1.tmx' definitely exists in my 'assets' folder inside the Android folders of the game hierarchy.

If you do need the rest of my code Im happy to put it up but there is lots of garbage there not related to the problem. Thanks for any help

EDIT: One thing i think it may be is the type of file Im making in Tiled Editor. (ie. I chose zlib compression) other options were XML CSV and i think gzip

  • ANOTHER EDIT -- i managed to view an error message at the bottom of Android Studio saying unable to load file "....tiles-brown.png" -- I was unaware I had to load those textures, the two tiles png files used are also in the assets folder anyway already but it says Couldn't Load on the errors. PLEASE HELP

Solution

  • You do not have to load the textures yourself. The loader will load all referenced textures for you, but you will have to put them in your assets folder at the same location which was used when creating the map with TilEd. Since you didn't do that right, you get the exception.

    A good way to do this is creating a folder levels and a folder textures in your assets directory. Put your .tmx files in levels and the tilesets in textures. Now when you import a tileset from TilEd, it will be referenced as ../textures/tileset.png. To make sure this is correct, you can check the .tmx file. Do not use absolute file paths!