Phaser API Documentation

  Version: 
fromJSON(x, y, config, [options], [addToWorld])

Description:

Creates a body using the supplied physics data, as provided by a JSON file.

The data file should be loaded as JSON:

preload ()
{
  this.load.json('ninjas', 'assets/ninjas.json);
}

create ()
{
  const ninjaShapes = this.cache.json.get('ninjas');

  this.matter.add.fromJSON(400, 300, ninjaShapes.shinobi);
}

Do not pass the entire JSON file to this method, but instead pass one of the shapes contained within it.

If you pas in an options object, any settings in there will override those in the config object.

The structure of the JSON file is as follows:

{
  'generator_info': // The name of the application that created the JSON data
  'shapeName': {
    'type': // The type of body
    'label': // Optional body label
    'vertices': // An array, or an array of arrays, containing the vertex data in x/y object pairs
  }
}

At the time of writing, only the Phaser Physics Tracer App exports in this format.

Parameters:

name type arguments Default description
x number

The X coordinate of the body.

y number

The Y coordinate of the body.

config any

The JSON physics data.

options Phaser.Types.Physics.Matter.MatterBodyConfig <optional>

An optional Body configuration object that is used to set initial Body properties on creation.

addToWorld boolean <optional> true

Should the newly created body be immediately added to the World?

Returns:
Description:

A Matter JS Body.

Type:
  • MatterJS.BodyType
Since: 3.22.0