简体   繁体   中英

Tiled Map Editor exporting Libgdx

I have a problem with exporting Tiled map file. I create map and , suggest a tutorial, I exporting tmx file and import it in Eclipse project assets. In the code I did this:

tiledMap = new TmxMapLoader().load("map.tmx");
tiledMapRenderer = new OrthogonalTiledMapRenderer(tiledMap);

My problem is that when I execute it I get this :

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: tileset 2/grass-tiles-2-small.png
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:140)
    at com.badlogic.gdx.graphics.glutils.FileTextureData.prepare(FileTextureData.java:64)
    at com.badlogic.gdx.graphics.Texture.load(Texture.java:130)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:121)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:100)
    at com.badlogic.gdx.graphics.Texture.<init>(Texture.java:96)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:116)
    at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:101)
    at com.mygdx.game.MyGdxGame.create(MyGdxGame.java:43)
    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: tileset 2/grass-tiles-2-small.png (Internal)
    at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
    at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:220)
    at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:137)

What do I for fix it?

As I see in libGdx wiki , there is two option to load tmx files. Make sure your files under the assets folder. You can see in libGdx Wiki how to load tmx files . Also there is similar question in here .

Tiled is referencing its tileset image source in the .tmx file. So the tileset grass-tiles-2-small.png you used is not embedded and has to be loaded, too. Fortunately this is done automatically by resolving the image dependencies for you.

So additionally to map.tmx you have to make sure that all used image files are reachable by copying them to your assets folder. It has to look like this: assets/tileset 2/grass-tiles-2-small.png . Refresh your Eclipse project after doing so, just in case it didn't notice.

If you already did that, another source of the problem could be the whitespace in your directory name. Try to rename tileset 2 to tileset_2 and create a new .tmx file.

It simply failed to load you texture grass-tiles-2-small.png . I usually open up the TMX/XML file and set the filenames for the tilesheets to just the filenames manually without a path.

Example:

<tileset firstgid="1" name="grass" tilewidth="32" tileheight="32">
  <image source="grass-tiles-2-small.png" width="256" height="256"/>
 </tileset>

You should put the textures in the same folder within your project as the map.tmx resides preferably Android/assets/../.. and use the Gdx.files.internal to load your map. internal just returns a regular file handle.

tiledMap = new TmxMapLoader().load(Gdx.files.internal("yourmap.tmx"));

The TMX just looks in the same folder for the right textures to use since we didn't specify a path in the tmx/xml.

Thoughts: I really hate the LibGDX default TmxLoader . It would work for really simple things but you have no way of sorting depth when sprites overlap since the only way to draw is to draw a complete layer at once. If you need a overlap on a player you need to be able to draw tile by tile/object by object. I wrote my own XML parser for Tiled maps and it surprised me how simple it was. I have a pretty complicated map and it just took me about 30 lines of code to load everything into an array ready to draw it within LibGDX .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM