# Can't add collision between layer and player

**URL:** <https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474>\
**Category:** Phaser 3\
**Created:** [May 16, 2019, 1:12pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474 "2019-05-16T13:12:59Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![tukanoidd](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tukanoidd](https://phaser.discourse.group/u/tukanoidd)\
**Post date:** [May 16, 2019, 1:12pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/1 "2019-05-16T13:12:59Z")

</div>

Hello, have problem written above. Tried to find solutions and failed to do so.

Here’s my code regarding tilemap and character:

```
function preload() {
    this.load.image('dungeonTiles', 'assets/tileset.png');
    this.load.tilemapTiledJSON('map', 'assets/map.json');
    this.load.spritesheet('player', 'assets/player_spritesheet.png', {
        frameWidth: 50,
        frameHeight: 37
    });
}

function create() {
    gameState.map = this.add.tilemap('map');
    gameState.dungeonTileset = gameState.map.addTilesetImage('dungeon', 'dungeonTiles');

    gameState.backgroundLayer = gameState.map.createStaticLayer('Background', gameState.dungeonTileset);
    gameState.mapLayer = gameState.map.createStaticLayer('Map', gameState.dungeonTileset);
    gameState.miscLayer = gameState.map.createStaticLayer('Misc', gameState.dungeonTileset);

    gameState.mapLayer.setCollisionByExclusion([-1]);

    this.physics.world.bounds.width = gameState.mapLayer.width;
    this.physics.world.bounds.height = gameState.mapLayer.height;

    gameState.player = this.physics.add.sprite(73, 398, 'player', 0);
    gameState.player.setCollideWorldBounds(true);

    this.physics.add.collider(gameState.mapLayer, gameState.player);
}

const config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: "#f",
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: gameState.gravity },
            debug: true
        }
    },
    scene: {
        preload,
        create,
        update
    }
};

```

Thanks in advance!!!

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [May 17, 2019, 4:35am UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/2 "2019-05-17T04:35:42Z")

</div>

Hi,  
Your snippet is very long. The best is just post the minimal snippet of your problem. And the top is make a jsfiddle with this snippet like this : [https://jsfiddle.net/espace3d/vLq08gke/12/](https://jsfiddle.net/espace3d/vLq08gke/12/)

So the others may help you 😉

Often when you do this exercice you identify yourself the problem!

---

<div class="post-metadata">

**Author:** ![tukanoidd](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tukanoidd](https://phaser.discourse.group/u/tukanoidd)\
**Post date:** [May 17, 2019, 3:17pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/3 "2019-05-17T15:17:03Z")

</div>

Yeah, sorry about that, changed.

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [May 17, 2019, 5:17pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/4 "2019-05-17T17:17:32Z")

</div>

Your collider must be in the update function.

---

<div class="post-metadata">

**Author:** ![tukanoidd](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tukanoidd](https://phaser.discourse.group/u/tukanoidd)\
**Post date:** [May 17, 2019, 5:34pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/5 "2019-05-17T17:34:36Z")

</div>

I read that phaser 3 collider checks collisions automatically after being initiated in **create** , no? Did it change again?

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [May 17, 2019, 5:41pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/6 "2019-05-17T17:41:09Z")

</div>

Yes i made a mistake. If i have time further i will see about your problem.

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [May 17, 2019, 5:44pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/7 "2019-05-17T17:44:57Z")

</div>

Have you see this example, maybe it could help you…  
[https://labs.phaser.io/edit.html?src=src/game%20objects\tilemap\collision\tiled%20map%20and%20impact.js](https://labs.phaser.io/edit.html?src=src/game%20objects%5Ctilemap%5Ccollision%5Ctiled%20map%20and%20impact.js)

---

<div class="post-metadata">

**Author:** ![espace](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/espace/32/1405_2.png) [@espace](https://phaser.discourse.group/u/espace)\
**Post date:** [May 17, 2019, 5:45pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/8 "2019-05-17T17:45:29Z")

</div>

Sorry it’s with impact js  
try this : [https://labs.phaser.io/edit.html?src=src/bugs\ap%20tilemap.js](https://labs.phaser.io/edit.html?src=src/bugs%5Cap%20tilemap.js)

---

<div class="post-metadata">

**Author:** ![jjcapellan](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/jjcapellan/32/542_2.png) [@jjcapellan](https://phaser.discourse.group/u/jjcapellan)\
**Post date:** [May 17, 2019, 7:46pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/9 "2019-05-17T19:46:45Z")

</div>

Hi @tukanoidd,  
Your code seems correct for me. Maybe the problem is in the “map.json” file.  
You could try to log one tile and check its index and collide properties. Check if tile index is allways -1.

Regards.

P.S.: Just one observation, you can use the keyboard.addKeys () method in the create () function only once.

---

<div class="post-metadata">

**Author:** ![tukanoidd](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tukanoidd](https://phaser.discourse.group/u/tukanoidd)\
**Post date:** [May 18, 2019, 3:06pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/10 "2019-05-18T15:06:36Z")

</div>

Tried it, but the problem is that because I have 3 layers, Tiled exports 3 different csv files, and when i tried to import all 3 of them and create layers out of them, only the last one of them was visible for some reason

---

<div class="post-metadata">

**Author:** ![tukanoidd](https://avatars.discourse-cdn.com/v4/letter/t/ba9def/32.png) [@tukanoidd](https://phaser.discourse.group/u/tukanoidd)\
**Post date:** [May 18, 2019, 3:07pm UTC](https://phaser.discourse.group/t/cant-add-collision-between-layer-and-player/2474/11 "2019-05-18T15:07:38Z")

</div>

Hey, thanks, but what exactly could be wrong with json file? What in your opinion can affect layer not to be collidable?
