Phaser API Documentation

  Version: 
createFromObjects(objectLayerName, config, [useTileset])

Description:

This method will iterate through all of the objects defined in a Tiled Object Layer and then convert the matching results into Phaser Game Objects (by default, Sprites)

Objects are matched on one of 4 criteria: The Object ID, the Object GID, the Object Name, or the Object Type.

Within Tiled, Object IDs are unique per Object. Object GIDs, however, are shared by all objects using the same image. Finally, Object Names and Types are strings and the same name can be used on multiple Objects in Tiled, they do not have to be unique; Names are specific to Objects while Types can be inherited from Object GIDs using the same image.

You set the configuration parameter accordingly, based on which type of criteria you wish to match against. For example, to convert all items on an Object Layer with a gid of 26:

createFromObjects(layerName, {
  gid: 26
});

Or, to convert objects with the name 'bonus':

createFromObjects(layerName, {
  name: 'bonus'
});

Or, to convert an object with a specific id:

createFromObjects(layerName, {
  id: 9
});

You should only specify either id, gid, name, type, or none of them. Do not add more than one criteria to your config. If you do not specify any criteria, then all objects in the Object Layer will be converted.

By default this method will convert Objects into Phaser.GameObjects.Sprite instances, but you can override this by providing your own class type:

createFromObjects(layerName, {
  gid: 26,
  classType: Coin
});

This will convert all Objects with a gid of 26 into your custom Coin class. You can pass any class type here, but it must extend Phaser.GameObjects.GameObject as its base class. Your class will always be passed 1 parameter: scene, which is a reference to either the Scene specified in the config object or, if not given, the Scene to which this Tilemap belongs. The class must have setPosition and setTexture methods.

Custom properties on the Object are copied onto any existing properties on the Game Object, so you can use this as an easy way to configure properties from within the map editor. For example giving an Object a property of alpha: 0.5 in Tiled will be reflected in the Game Object that is created.

Custom properties that do not exist on the Game Object are set in the Game Object's data.

When useTileset is true (the default), Tile Objects will inherit the texture and any tile properties from the tileset, and the local tile ID will be used as the texture frame. For the frame selection to work you need to load the tileset texture as a spritesheet so its frame names match the local tile IDs.

For instance, a tileset tile

{ id: 3, type: 'treadmill', speed: 4 }

with gid 19 and an object

{ id: 7, gid: 19, speed: 5, rotation: 90 }

will be interpreted as

{ id: 7, gid: 19, speed: 5, rotation: 90, type: 'treadmill', texture: '[the tileset texture]', frame: 3 }

You can suppress this behavior by setting the boolean ignoreTileset for each config that should ignore object gid tilesets.

You can set a container property in the config. If given, the new Game Object will be added to the Container or Layer instance instead of the Scene.

You can set named texture-key and texture-frame properties, which will be set on the new Game Object.

Finally, you can provide an array of config objects, to convert multiple types of object in a single call:

createFromObjects(layerName, [
  {
    gid: 26,
    classType: Coin
  },
  {
    id: 9,
    classType: BossMonster
  },
  {
    name: 'lava',
    classType: LavaTile
  },
  {
    type: 'endzone',
    classType: Phaser.GameObjects.Zone
  }
]);

The signature of this method changed significantly in v3.60.0. Prior to this, it did not take config objects.

Parameters:

name type arguments Default description
objectLayerName string

The name of the Tiled object layer to create the Game Objects from.

config Phaser.Types.Tilemaps.CreateFromObjectLayerConfig | Array.<Phaser.Types.Tilemaps.CreateFromObjectLayerConfig>

A CreateFromObjects configuration object, or an array of them.

useTileset boolean <optional> true

True if objects that set gids should also search the underlying tile for properties and data.

Returns:
Description:

An array containing the Game Objects that were created. Empty if invalid object layer, or no matching id/gid/name was found.

Since: 3.0.0
Source: src/tilemaps/Tilemap.js (Line 628)