Scratch for Java

TiledMap::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, and map.spawn("Objects", Player.class) makes a new 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);

Examples

File /src/examples/java/reference//TiledSpawn.java is missing.

View on GitHub

Syntax

Java

.spawn(layerName, types)

Parameters

Name Data Type Description
layerName String the object layer
types Class<Sprite> the sprite classes to make, matched to the objects' types by their names

Return

List<Sprite>

spawn()

Create Shareable URL

Select Sections