Jump to content

(Newbie) Sprite Doesn't Collide with Tilemap Layer


mksas
 Share

Recommended Posts

Hi,

I'm a complete newbie in Phaser and I am struggling with tilemap layer collisions. So I have this tilemap with 2 layers: background, and collision (lower case), and I want to make my sprite (the player) collide with the collision layer, but it just doesn't seem to work. When I walk over it with the player I can just continue to walk even though I'm standing in the wall. It does load all the sprites and tilemap layers, but it just doesn't collide with the collision layer. I hope someone can help, thanks in advance.

var game = new Phaser.Game(800,600,Phaser.AUTO,'', {preload: preload, create: create, update: update});function preload() {    game.load.tilemap('hallway', 'assets/hallway.json', null, Phaser.Tilemap.TILED_JSON);    game.load.image('tiles', 'assets/tiles.png');    game.load.image('player', 'assets/star.png');}var map, background, collision, cursors, player;function create() {    game.physics.startSystem(Phaser.Physics.ARCADE);    map = game.add.tilemap('hallway');    map.addTilesetImage('tiles');    background = map.createLayer('background');    collision = map.createLayer('collision');    map.setCollision(13);    background.resizeWorld();    collision.resizeWorld();    player = game.add.sprite(550,30,'player');    game.physics.arcade.enable(player);    player.body.collideWorldBounds = true;    game.camera.follow(player);    cursors = game.input.keyboard.createCursorKeys();}function update() {    game.physics.arcade.collide(player, collision);    player.body.velocity.x = 0;    player.body.velocity.y = 0;    if (cursors.left.isDown)    {        //  Move to the left        player.body.velocity.x = -150;    }    else if (cursors.right.isDown)    {        //  Move to the right        player.body.velocity.x = 150;    }    else if (cursors.up.isDown)    {        //  Move to the right        player.body.velocity.y = -150;    }    else if (cursors.down.isDown)    {        //  Move to the right        player.body.velocity.y = 150;    }}
Link to comment
Share on other sites

In your call to setCollision, pass in the collision layer as the third parameter. It'll look like "map.setCollision(13, true, collision);". See if that fixes it. You also only need one call to resizeWorld (probably on your collision layer).

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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