Jump to content

How Do I Handle Collision With a Layer?


Recommended Posts

Hello all,

I am currently trying to make a simple platformer, but am having trouble detecting collision with a tile layer.  

After looking through many of the other forum posts here about this problem, none of the fixes seem to work for me.  'World 1' layer is the background and 'Collision' is the layer that contains the platforms.  

To my understanding, I should just be able to use map.setCollisionBetween() on the tiles that I want to use as the ground, which I think I did correctly.  I also made sure to enable physics for the player and the collision layer.  For some reason, though when I console.log(game.physics.arcade.collide(player, col)), it always returns false.  When I run console.log(player.body.blocked), it only returns true when the player is on the window boundary (because of collideWorldBounds=true).

Can anyone see what I'm doing wrong?

var game = new Phaser.Game(800, 480, Phaser.AUTO, 'phaser-example', { preload: preload, create: create, update: update });

function preload() {

    game.load.tilemap('bgmap', 'assets/tilemaps/maps/superMarioBG.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('bgtiles', 'assets/tilemaps/tiles/super_mario.png');

    game.load.image('player', 'assets/sprites/georgeRight.png');

var map;
var bg;
var col;
var player;
var cursors;
var jumpButton;

function create() {

    game.stage.backgroundColor = '#787878';

    map = game.add.tilemap('bgmap');
    map.addTilesetImage('SuperMarioBros', 'bgtiles');
    bg = map.createLayer('World1');
    col = map.createLayer('Collision');

    player = game.add.sprite(0,game.world.height - 150,'player');
    player.scale.set(1.5) ;
    cursors = game.input.keyboard.createCursorKeys();
    jumpButton = game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR);

    player.body.bounce.y = 0.2;
    player.body.gravity.y = 500;
    player.body.collideWorldBounds = true;
function update() {
    game.physics.arcade.collide(player, col);
    if (cursors.left.isDown)
        player.body.velocity.x -= 500;
    else if (cursors.right.isDown)
        player.body.velocity.x += 500;
    if (jumpButton.isDown && player.body.onFloor()) {
        player.body.velocity.y = -550;

    function render() {


Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


  • Recently Browsing   0 members

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