Jump to content

Ground collision with tilemap


Pau
 Share

Recommended Posts

Hello!

I am trying to make a player to collide with the ground. But the ground has been imported from a tilemap using the tiled program. The collision is not working :(

This is the important part of my code:
 

collisionLayer = map.createStaticLayer('collisionLayer', tileset);

var players = findObjectsByType('player', map, 'objectsLayer');
player = this.physics.add.sprite(players[0].x, players[0].y, 'dude');
this.physics.add.collider(player, collisionLayer);

findObjectsByType is not a phaser native method, but it is working.

The code runs without errors, but the collision is not working.

I have attached the code.

Thanks!

example.zip

Link to comment
Share on other sites

  • 2 months later...

How did you determine that his collision tile id is 3? I'm having a similar problem - no matter what I've tried I can't get my player to collide with the layer:

var map = this.make.tilemap({
    key: 'map'
});

var collisionTiles = map.addTilesetImage("collisions");
var groundTiles = map.addTilesetImage("tileset");

var collisionLayer = map.createStaticLayer('Collisions Layer', collisionTiles);  
var layer = map.createStaticLayer('Tile Layer', groundTiles);
var layer2 = map.createStaticLayer('Tile Layer 2', groundTiles);
    
collisionLayer.setCollisionBetween(1, 1);

player = this.physics.add.sprite(460, 380, 'hero');
player.setCollideWorldBounds(true);

this.physics.add.collider(player, collisionLayer);

My map file is here:

https://github.com/bhamlin3/bow-hunter/blob/master/public/assets/maps/map.json

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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