Jump to content

Tilemap tiles not colliding with sprite


Arcanorum
 Share

Recommended Posts

So I'm trying to enable collision between my player's sprite and a tilemap.

The JSON map (made in Tiled) has multiple layers. Some of the layers use some of the same tiles for an overlaying effect.

 

I've looked at this example and have done as it does but I can't get any collision working for any of the layers in the tilemap. The player just moves straight through all of the tile IDs that I have set for collision with .setCollision unhindered.

 

For those wondering, the player sprite is moving with velocity using .moveToPointer( ... ), not absolute transforms.

 

Le code:

var map;var layerBackground;var layerObjects1;var layerObjects2;var layerObjects3;function preload(){    game.load.tilemap('firstMap', 'assets/maps/Magic Map.json', null, Phaser.Tilemap.TILED_JSON);    game.load.image('spriteSheet', 'assets/maps/map_spritesheet.png');    ...}function create(){    game.physics.startSystem(Phaser.Physics.ARCADE);    map = game.add.tilemap('firstMap');    map.addTilesetImage('map_spritesheet', 'spriteSheet');    // Not sure how Phaser handles the same tile being used on    // multiple tilemap layers, so I try them all just in case.    // #23 on my tileset represents a big rock that I want collision for.    map.setCollision(23, true, layerObjects1);    map.setCollision(23, true, layerObjects2);    map.setCollision(23, true, layerObjects3);    // Are there any special rules for putting all the layers of    // a tilemap into a game.add.group()?    layerBackground = map.createLayer('Background');    layerObjects1 = map.createLayer('Objects1');    layerObjects2 = map.createLayer('Objects2');    layerObjects3 = map.createLayer('Objects3');    // I have seen some other people that used multiple layers    // call .resizeWorld() for each layer. Is this necessary?    // I assumed it should only be done once (i.e. the biggest layer).    layerBackground.resizeWorld();    ...}function update(){    // I'm guessing having all the layers in a group would    // make checking collision neater.    game.physics.arcade.collide(player.sprite, layerObjects1);    game.physics.arcade.collide(player.sprite, layerObjects2);    game.physics.arcade.collide(player.sprite, layerObjects3);    ...}

Are there any obvious errors with what I'm doing? It all looks legit to me from my understanding of the documentation and examples, but still no success. =(

 

I've already wasted a reasonable amount of time on this problem. Thanks for any help.

Link to comment
Share on other sites

I think the problem in 

map.addTilesetImage('map_spritesheet', 'spriteSheet'); 

What is "map_spritesheet"?

 

In your preload method you have the "spriteSheet" image:

game.load.image('spriteSheet', 'assets/maps/map_spritesheet.png');

Maybe you need to swap properties, or remove the second property:

map.addTilesetImage('spriteSheet', 'map_spritesheet'); 

addTilesetImage method takes tileset name as first property

addTilesetImage(tileset, key, tileWidth, tileHeight, tileMargin, tileSpacing, gid)

http://docs.phaser.io/Phaser.Tilemap.html#addTilesetImage

 

I'm not sure, but you can try..

Link to comment
Share on other sites

The map all shows up correctly so it can't be that.

map_spriteSheet is just the file name of the tileset I'm using.

 

I thought maybe the problem was that map couldn't find the right tileset ID to check for collision against, but if that was the case then the map wouldn't show up at all.

 

The only thing I can think of is something to do with there being multiple layers?

Link to comment
Share on other sites

I figured it out.

 

I was using the vars that held the TilemapLayer objects as the 'layer' parameter in setCollision, since it does ask for a TilemapLayer as the layer parameter. What I derped on was the fact that I should have created the layers before I used setCollision.

 

As a side note I found that using the actual name of the layer (as it appears in the tilemap data) as the parameter also works, which means the order no longer matters.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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