# How to add Tiled objects in Phaser with custom class?

**URL:** <https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847>\
**Category:** Phaser 3\
**Created:** [October 22, 2020, 9:44am UTC](https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847 "2020-10-22T09:44:58Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pierre.josselin](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@pierre.josselin](https://phaser.discourse.group/u/pierre.josselin)\
**Post date:** [October 22, 2020, 9:44am UTC](https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847/1 "2020-10-22T09:44:59Z")

</div>

**How can I add coins with position, image and collisions as defined in Tiled, but using my custom class Game.Unit.Coin?**

I created a Tiled map with two layers:

- “coins” objects
- “walls” tiles

[Tiled screenshot](https://i.stack.imgur.com/oXM7D.png)

I defined custom collisions on coins:

[Tiled screenshot](https://i.stack.imgur.com/Q0XSI.png)

I created a custom Coin class in Tiled:

```
Game.Unit.Coin = class extends Phaser.Physics.Matter.Sprite {
    constructor(scene, x, y, texture) {
        super(scene.matter.world, x, y, texture);
        scene.add.existing(this);
    }
}

```

Here is my Phaser code:

```
// Loading assets
this.load.image("walls", "assets/tilesets/walls.png");
this.load.image("coins", "assets/tilesets/coins.png");
this.load.tilemapTiledJSON("test", "assets/maps/test.json");

// Creating map
var map = this.make.tilemap({key: "test"});

// Creating walls
var wallsTileset = map.addTilesetImage("tileBlack_02", "walls");
var wallsLayer = map.createStaticLayer("walls", wallsTileset, 0, 0);
wallsLayer.setCollisionBetween(1, 11);
this.matter.world.convertTilemapLayer(wallsLayer);

// Creating coins
var coinsTileset = map.addTilesetImage("spritesheet_coins", "coins");
// ???

```

**Version**  
Tiled _1.4.2_  
Phaser _3.24.1_

---

<div class="post-metadata">

**Author:** ![Milton](https://avatars.discourse-cdn.com/v4/letter/m/87869e/32.png) [@Milton](https://phaser.discourse.group/u/Milton)\
**Post date:** [October 23, 2020, 7:52am UTC](https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847/2 "2020-10-23T07:52:34Z")

</div>

Maybe have a look at [this](https://phaser.discourse.group/t/how-to-create-object-layer-from-tiled-tile-objects/7362) topic?  
I don’t think it’s exactly what you’re looking for, but maybe it’ll give you some pointers.

---

<div class="post-metadata">

**Author:** ![pierre.josselin](https://avatars.discourse-cdn.com/v4/letter/p/f9ae1b/32.png) [@pierre.josselin](https://phaser.discourse.group/u/pierre.josselin)\
**Post date:** [October 23, 2020, 8:48am UTC](https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847/3 "2020-10-23T08:48:02Z")

</div>

Thank you for your reply, i will watch this.

---

<div class="post-metadata">

**Author:** ![Telinc1](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/telinc1/32/60_2.png) [@Telinc1](https://phaser.discourse.group/u/Telinc1)\
**Post date:** [October 23, 2020, 10:04am UTC](https://phaser.discourse.group/t/how-to-add-tiled-objects-in-phaser-with-custom-class/7847/4 "2020-10-23T10:04:17Z")

</div>

It’s worth mentioning that Phaser 3.50 will make the `createFromObjects` method on tilemaps more powerful. One of the new features is the ability to instantiate a custom class. Currently, however, I think the only way to do this is to manually iterate the object layer.
