# Tilemap collision area best practises

## Recommended Posts

Hello,

Beginner user here learning the basics. My guestion is about what is good way to use tilemaps for collision areas. Image from my training project:

I've created this level with Tiled using one layer, and then defining it like this in game.js:

`this.map.setCollision([0, 0, 0, 0,.. 1]) // removed lot's of data to save space`

Well, that works, but somehow i feel there has to be better way? I also saw somebody doing it very wisely with p2 physics, and drawing collision areas with one tile and then in the code using tile's id to set collision areas. I liked this approach, but i never got it working, and i'm using arcade physics for now.

Like i mentioned this is just practice, but i like to know some best pratices before i start doing my first game

##### Share on other sites

The setCollision method of the Tilemap take an array of tile indexes and so you only need to have the zero once. The index refers to the index of the tile in the tileset, not the index of the tile in the Tilemap data. So I see that you have 2 types of tiles, so you only need an array with theses two indexes

`this.map.setCollision([0,1]);`

Also, separating your collision layers from other layers is a good idea, so you are off to a good start. =) For other best practices, check out the official Tilemap examples, they may not be all that fancy but they cover a lot of the basics that should be great for a beginner. =)

##### Share on other sites

The setCollision method of the Tilemap take an array of tile indexes and so you only need to have the zero once. The index refers to the index of the tile in the tileset, not the index of the tile in the Tilemap data. So I see that you have 2 types of tiles, so you only need an array with theses two indexes

`this.map.setCollision([0,1]);`

Also, separating your collision layers from other layers is a good idea, so you are off to a good start. =) For other best practices, check out the official Tilemap examples, they may not be all that fancy but they cover a lot of the basics that should be great for a beginner. =)

Yeah, i've seen examples using setCollision in this way, but on my case if use :

`this.map.setCollision([0,1]);`

My player only collides with the floor, not the boxes.

EDIT:

Ah, i used wrong numbers, this will make it work properly, yei!:

`this.map.setCollision([1, 3])`