Class TiledMap
java.lang.Object
org.openpatch.scratch.tiled.TiledMap
A map made with the Tiled editor (TMX or TMJ): its tile layers are shown in the stage's layers
and drawn every frame, its object layers can spawn sprites, its tiles carry properties and
collision shapes.
Example usage:
TiledMap map = new TiledMap("assets/map.tmx", stage);
map.showLayer("ground", Layer.BACKGROUND);
map.showLayer("treetops", Layer.FOREGROUND);
for (MapObject object : map.getObjectsFromLayer("objects")) {
if (object.type.equals("player")) {
Player p = new Player(object.x, object.y);
stage.add(p);
}
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionintgetObjectsFromLayer(String name) Retrieves the objects from a specified layer in the tiled map.getProperty(String name) Reads a custom property of the map itself, such as its biome or tutorial.getProperty(String name, String defaultValue) Reads a map property, with a default for maps that omit it.booleangetPropertyBoolean(String name) floatgetPropertyFloat(String name) intgetPropertyInt(String name) Shape[]getTileCollisionShapes(int tileId) Returns collision outlines defined in a tile's tileset object group.Shape[]getTileCollisionShapes(String layerName, int column, int row) Returns a cell's collision outlines in map/stage coordinates: the map's top-left is (0,0), x points right and y points up, matching map stamping.intintReads a global tile ID, without flip bits.intintgetWidth()voidStops drawing a tile layer shown withshowLayer(String, Layer).voidShows a tile layer of the map in a layer of the stage, drawn every frame from now on (only the tiles the camera shows).Creates a sprite for each object of a layer whose type is one of the given sprite classes, and adds it to the stage.toString()
-
Constructor Details
-
TiledMap
Loads map data without attaching it to a stage. Properties, tile IDs and collision shapes can be read without a window. To stamp or spawn, use the constructor that takes a stage.- Parameters:
path- a TMX file
-
TiledMap
-
-
Method Details
-
getWidth
public int getWidth()- Returns:
- the number of map columns
-
getHeight
public int getHeight()- Returns:
- the number of map rows
-
getTileWidth
public int getTileWidth()- Returns:
- the width of a map cell in pixels
-
getTileHeight
public int getTileHeight()- Returns:
- the height of a map cell in pixels
-
getProperty
Reads a custom property of the map itself, such as its biome or tutorial.- Parameters:
name- property name- Returns:
- its string value, including multiline strings
- Throws:
NoSuchElementException- if the property does not exist
-
getProperty
-
getPropertyInt
- Parameters:
name- property name- Returns:
- its integer value
-
getPropertyFloat
- Parameters:
name- property name- Returns:
- its floating-point value
-
getPropertyBoolean
- Parameters:
name- property name- Returns:
- its boolean value
-
getTileId
Reads a global tile ID, without flip bits. Columns and rows are local to the named layer, starting at its top-left cell. Zero means an empty cell, including cells outside the layer. An unknown layer is an error.- Parameters:
layerName- tile layer name, also within a groupcolumn- column indexrow- row index- Returns:
- global ID, or zero for an empty/outside cell
-
getTileCollisionShapes
Returns collision outlines defined in a tile's tileset object group. Coordinates start at the tile's top-left; x points right and y points up. Shapes are independent copies and do not modify the map when transformed.- Parameters:
tileId- global tile ID (zero and unknown IDs have no shapes)- Returns:
- rectangles, ellipses or polygons for this tile
-
getTileCollisionShapes
Returns a cell's collision outlines in map/stage coordinates: the map's top-left is (0,0), x points right and y points up, matching map stamping. Tiled's diagonal, horizontal and vertical flips are applied in that order.- Parameters:
layerName- tile layer namecolumn- column index, starting at zerorow- row index, starting at zero- Returns:
- positioned outlines, or an empty array outside/without collision
-
spawn
Creates a sprite for each object of a layer whose type is one of the given sprite classes, and adds it to the stage. In Tiled, give an object the type (called "class" in newer versions of Tiled)Player, andmap.spawn("Objects", Player.class)makes anew Player()where the object is.The sprite's class needs one of these constructors, tried in this order: one that takes the
MapObject(to read its name and properties), one that takes(double x, double y), or one without parameters. Either way the sprite is put where the object is. An object drawn as an area (a rectangle, an ellipse or a polygon) also gives the sprite that area as its hitbox, which is how walls and water work without a costume.Objects whose type is none of the classes, or that have no type, are left alone, so a layer can hold markers the program reads itself. The classes are named in the program, not looked up by the names in the map: a game exported as a web page only contains the classes its code names.
TiledMap map = new TiledMap("level.tmx", this); map.spawn("Objects", Player.class, Wall.class);- Parameters:
layerName- the object layertypes- the sprite classes to make, matched to the objects' types by their names- Returns:
- the sprites that were made, in the order of the layer
- Throws:
IllegalArgumentException- if no class is given, or the map has no such object layer
-
getObjectsFromLayer
-
showLayer
Shows a tile layer of the map in a layer of the stage, drawn every frame from now on (only the tiles the camera shows). The map's top left corner is at (0, 0) of the world; the map extends right and down.map.showLayer("ground", Layer.BACKGROUND); map.showLayer("trees", Layer.FOREGROUND);- Parameters:
name- the tile layer's name in Tiledlayer- the stage's layer to draw it in
-
hideLayer
Stops drawing a tile layer shown withshowLayer(String, Layer).- Parameters:
name- the tile layer's name in Tiled
-
toString
-