Search code examples
javalibgdxtiled

libGDX load tiled map from string


I currenly load my tiled map from assets folder.

TiledMap tileMap = new TmxMapLoader().load("assets/level1.tmx");

But I want to make something like level of the day. Is it possible to load a tiled map from a string?

A string would be the content of a .tmx file.

Example http://pastebin.com/WpV90Hma


Solution

  • The easiest way would probably be to create a (temporary) FileHandle with the content and use that to load the map.

    By default TmxMapLoader will use an InternalFileHandleResolver. This won't work, because you cannot create internal files at runtime.

    That's why you would instead use an ExternalFileHandleResolver for the map loader, create an external file and write your map of the day as a string into it.

    String mapOfTheDay = ...;
    FileHandle mapOfTheDayFile = Gdx.files.external("mygame/mapoftheday.tmx");
    mapOfTheDayFile.writeString(mapOfTheDay, false);
    TiledMap tileMap = new TmxMapLoader(new ExternalFileHandleResolver()).load("mygame/mapoftheday.tmx");