Search code examples
javalibgdxtmx

Unable to load tiled map (Libgdx)


I've tried using different tiled maps and using the zlib compression type. I have the png file and the tmx file inside the assets folder under android.

If you need anymore information I will reply. Any help is appreciated. Thanks

package com.mm.test1;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;

public class TestOne extends ApplicationAdapter {
    private TiledMap map;
    private OrthogonalTiledMapRenderer renderer;

    @Override
    public void create () {
        map = new TmxMapLoader().load("grass1.tmx");
        renderer = new OrthogonalTiledMapRenderer(map);
    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0,0,0,1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        renderer.render();
    }

    @Override
    public void dispose () {
        map.dispose();
        renderer.dispose();
    }
}

Heres the error message:

  Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: Couldn't load file: Downloads/TilesetGrass/TilesetGrass/overworld_tileset_grass.png
        at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:149)
        at com.badlogic.gdx.graphics.TextureData$Factory.loadFromFile(TextureData.java:98)
        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:86)
        at com.badlogic.gdx.maps.tiled.TmxMapLoader.load(TmxMapLoader.java:67)
        at com.mm.test1.TestOne.create(TestOne.java:16)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:149)
        at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:126)
    Caused by: com.badlogic.gdx.utils.GdxRuntimeException: File not found: Downloads\TilesetGrass\TilesetGrass\overworld_tileset_grass.png (Internal)
        at com.badlogic.gdx.files.FileHandle.read(FileHandle.java:136)
        at com.badlogic.gdx.files.FileHandle.readBytes(FileHandle.java:222)
        at com.badlogic.gdx.graphics.Pixmap.<init>(Pixmap.java:146)
        ... 8 more

Solution

  • The problem lies in the .tsx (TileSet) file.
    Go into the .tsx file and change:
    <image source="Downloads/TilesetGrass/TilesetGrass/overworld_tileset_grass.png" ... />
    to
    <image source="[relative path to your .png from asset folder]" ... />

    By creating your TileSet for your TileMap you probably have choose the .png when it was still in the download folder, so your TileSet points to the Path:Downloads/TilesetGrass/TilesetGrass/overworld_tileset_grass.png shown in your error message.