I am trying to get collisions working for my Tiled imported tilemap. Here are the steps I have taken so far:
Created a tileset in Tiled
Added a custom boolean property called “collides” set to true on the collidable tiles in the tileset
Created a tilemap (with the tileset embedded in the map) in Tiled and populated it with the tileset
Created separate Tile Layers, with a layer called Solid for collidable tiles only
Exported the tilemap as json
Put the tileset in a separate tilemap, so that I could export the tileset as an image.
After all this, I can successfully read in the tilemap and tileset into my game and everything looks good. I can even read in each of the Tile Layers created in Tiled with success.
However, the issue comes when I try to make the player collide with the tiles in the Solid layer. Here is how I attempt to do so:
In all the examples I have seen online, this code should make the player collide with the tiles with the collides property set to true. In the json file that my tilemap is referencing, I can see that the collides property is in fact being read in and for the correct tiles. I have even tried manually setting the collision tiles with this.solidLayer.setCollision([927, 928]), as I can see from the json that those tiles have the collides set to true. However, this still does not work.
In debugger mode there aren’t even any hitboxes being created anywhere on the Solid layer tiles.
I wanted to make sure the layers were actually being read, so I changed the offset of the layer’s coords with this.solidLayer = this.map.createLayer('Solid', this.tileset, 25, 25), and confirmed that the layers are properly being read.
Below is be an example of collision working properly in my game, however this is with another gameobject. Here, the collision is registered when the pink boxes overlap.
I have noticed that with the debug render, there is a sort of opaque filter over the tilemap layers and the player, however not the other gameobjects. Because of this I tried troubleshooting the z-index of the player and the layer, but that does not seem to change anything.
got it working! marking @samme’s last comment as solution.
the reason my player was not colliding was due to the fact that i was directly changing the player’s x and y positions. by instead using player.setVelocityX() and player.setVeloctyY(), the collisions began to work.
another issue i was having that convinced me that the issue was with the tilemap, was that my “overhead” layer was not actually appearing overhead of the player (i.e. the player was still in front of the overhead tiles, rather than the overhead tiles appearing in front of the player).
this was because i was reading in the layer correctly, but setting its depth incorrectly using layer.setZ(). the correct way to change the depth of a tilemap layer is by using the function layer.setDepth().