Phaser API Documentation

  Version: 
createFromObjects(objectLayerName, config)

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 3 criteria: The Object ID, the Object GID or the Object Name.

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

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, 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 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.

All properties from object are copied into 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 object properties that do not exist as a Game Object property are set in the Game Objects data.

You can use set a container property in the config. If given, the class will be added to the Container instance instead of the Scene.

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
  }
]);

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

Parameters:

name type 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.

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 634)