Scratch for Java

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 Wall or Player.

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. Use Layer.FOREGROUND for treetops the player walks under.
  • spawn("Objects", Wall.class, Water.class) makes a sprite for every object whose class is Wall or Water. A sprite made from a rectangle gets the rectangle as its hitbox, so walls need no costume.
  • getObjectsFromLayer("Start") gives you the objects themselves. x and y are 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

Maps with Tiled

Create Shareable URL

Select Sections