Class TiledImport

java.lang.Object
com.codename1.gaming.platformer.TiledImport

public final class TiledImport extends Object

Turns a map drawn in the Tiled editor into a level, so levels can be made in the tool most 2D games already use.

Export the map as JSON (.tmj, or .json) with embedded tilesets and CSV layer data (Tiled's defaults for JSON). Then:

import com.codename1.gaming.platformer.*;
import com.codename1.io.Util;

String tmjText = Util.readToString(Display.getInstance().getResourceAsStream(null, "/map.tmj"));
LevelData level = LevelData.parse(TiledImport.toLevelJson(tmjText));

What is read:

  • The collision layer: the tile layer named collision, or else the first tile layer. A tile's kind comes from a collision property on the tile in its tileset: solid, oneway, slope_up, slope_down, ladder, hazard, water or empty. Tiles without one are solid.
  • Other tile layers are art only and are listed under "layers", rows of global tile ids, for a game's own renderer to draw.
  • Objects, from every object layer. An object's class (Tiled 1.9+) or type names what it is: spawn, goal (its rectangle in whole tiles), or any entity type LevelData or the game's EntityFactory knows -- coin, enemy, crate, door and the rest. Its custom properties become the entity's fields, so an enemy's brain or a door's key are set in Tiled.
  • Map properties become the level's header: name, world, lives, players, health, par, and abilities as a comma-separated list.

Not supported, and reported as an error rather than read wrongly: infinite maps, base64 or compressed layer data and external tilesets.

  • Method Details

    • toLevelJson

      public static String toLevelJson(String tmj)

      Converts a Tiled JSON map to level JSON.

      Parameters
      • tmj: the map, as Tiled writes it
      Returns

      the level JSON, for LevelData#parse(String)

      Throws
      • IllegalArgumentException: when the map uses something not supported