Maps with Tiled
Tiled is a free editor for game maps. You paint a level from small square pictures, the tiles, and mark places with objects: walls, water, where the player starts. Scratch for Java shows the map and turns the objects into sprites.
A map in Tiled
A map has layers, like a drawing program:
- Tile layers are what you see: the floor, walls, trees.
-
Object layers hold invisible shapes with a name and a class (called
"type" in older Tiled versions), like
WallorPlayer.
Save the map as .tmx in your project, next to the tileset pictures it uses.
Showing the map
The map below has the tile layers Floor, FloorObjects and Walls, an object
layer Objects with walls and water, and an object layer Start with one
object, where the player starts. Use the arrow keys:
new TiledMap("Level1.tmx", this)loads the map for this stage.-
showLayer("Floor", Layer.BACKGROUND)draws a tile layer behind the sprites. UseLayer.FOREGROUNDfor treetops the player walks under. -
spawn("Objects", Wall.class, Water.class)makes a sprite for every object whose class isWallorWater. A sprite made from a rectangle gets the rectangle as its hitbox, so walls need no costume. -
getObjectsFromLayer("Start")gives you the objects themselves.xandyare already stage positions.
Turn on debug mode with F12 to see the walls' hitboxes.
Properties
In Tiled you can give a map or an object your own properties, like the name of
the next level. Read them with getProperty("next"), getPropertyInt("coins")
and getPropertyBoolean("locked"), on the map or on a MapObject.
A spawned sprite can read its object's properties too. Give its class a
constructor that takes the MapObject:
import org.openpatch.scratch.*;
import org.openpatch.scratch.tiled.*;
class Door extends Sprite {
private String next;
public Door(MapObject object) {
this.next = object.getProperty("next");
}
}
Next
- Tiled example: two levels, items, enemies and saving
- TiledMap in the reference