Jump to content

cdrch
 Share

Recommended Posts

I'm relatively new to Phaser (only a few months of experience with it), so it didn't come up until recently that I attempted using tilemaps (which in this case are JSON files generated with Tiled) that have multiple layers. My code works fine with a single layer, but all collision between a layer and anything else stops working when I modify the code to add a second layer. In addition, creating sprites from the object layer (createFromObject) doesn't seem to work either.

 

In my simplest test, the only change was adding a second layer to the map file (both layers share a single tileset), and the only change in the code is adding:

 

var layer2;

layer2 = map.createLayer('Tile Layer 2');

 

Both layers display properly, but collision stops working. I thought I had read that Phaser could handle multiple tile layers now. Is there something I'm missing that I need to have to get this to work?

Link to comment
Share on other sites

I've managed to fix the problem. For anyone else having the same issue, what fixed it for me was passing the layer you intend to collide with as an argument when declaring which tiles are being collided with. In addition, as far as I can tell, the layer needs to actually be declared before you can declare what tiles collide, so the setCollision call should come after the layer. If it comes immediately after, it shouldn't need to pass the layer name, as that defaults to map.currentLayer, the most recently modified layer.

 

So this did not work:

 

this.map.setCollision(1);

this.layerC = this.map.createLayer('Collision');

 

And this is what did:

 

this.layerC = this.map.createLayer('Collision');

this.map.setCollision(1, true, this.layerC);

 

And this should work as well:

 

this.layerC = this.map.createLayer('Collision');

this.map.setCollision(1);

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...