Jump to content

Phaser + Tiled, can't get collision to work.


hustlerinc
 Share

Recommended Posts

I'm having trouble with collisions between the player sprite and a tilemap layer created with Tiled Map Editor. The layer is drawn on the screen, and if I turn on debug I see the hitboxes highlighted but there's no collisions.

 

Here's the relevant code:

var game = new Phaser.Game(480, 320, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() {	game.load.spritesheet('player', 'assets/characters.png', 16, 16);	game.load.image('tiles', 'assets/tiles.png');	game.load.tilemap('map', 'assets/map.json', null, Phaser.Tilemap.TILED_JSON);}function create() {	map = game.add.tilemap('map');	map.addTilesetImage('tiles', 'tiles');		bgLayer = map.createLayer('bgLayer');	collisionLayer = map.createLayer('collisionLayer'); // player should collide with anything on this layer		map.setCollisionBetween(0, 200, true, 'collisionLayer');		player = game.add.sprite(0, 304, 'player'); // this sprite has to collide with collisionLayer		game.physics.arcade.enable(player);	player.body.collideWorldBounds = true;		collisionLayer.debug = true;}function update() {	game.physics.arcade.collide(player, collisionLayer);	playerMove();}

I'm sure I'm doing something wrong, but I don't know what.

Link to comment
Share on other sites

Hello,

 

 

I'm currently working on a tilemap as well, so I'll try to be of some assistance if possible.

 

First, you're not resizing the map to fit the backgroundLayer, which you probably should do.

bglayer.resizeWorld();

Second, make sure you enable the physics system at the start of the game:

game.physics.startSystem(Phaser.Physics.ARCADE);

I'm not sure if this step is necessary, but I specified the body size for my player sprite

player.body.setSize(24, 24);

Let me know if any of those steps help in resolving the issue. If not, we can try digging a little deeper.

Link to comment
Share on other sites

Hello,

 

 

I'm currently working on a tilemap as well, so I'll try to be of some assistance if possible.

 

First, you're not resizing the map to fit the backgroundLayer, which you probably should do.

bglayer.resizeWorld();

Second, make sure you enable the physics system at the start of the game:

game.physics.startSystem(Phaser.Physics.ARCADE);

I'm not sure if this step is necessary, but I specified the body size for my player sprite

player.body.setSize(24, 24);

Let me know if any of those steps help in resolving the issue. If not, we can try digging a little deeper.

Hi, thanks for helping me. I'm resizing the map, just didn't want to confuse with too much code in the post. Anyway I added

game.physics.startSystem(Phaser.Physics.ARCADE);

to my create function right before the call to map.setCollisionBetween(); and I added

player.body.setSize(16, 16);

after the line enabling arcade physics on the player (also in the create function). Same issue though, I can walk through the tiles I'm supposed to collide with.

 

If it helps here are the tutorials I've read, and I've done pretty much what they do only different ways of setting up the preload, create and update functions.

Do you have any other ideas why it doesn't work for me?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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