Jump to content

Can't get collision to work with tilemap layer


telmo
 Share

Recommended Posts

I've been trying for hours, read many of the examples and help posts but I still don't understand why my code doesn't work. If I create an object and force collision with "game.physics.arcade.collide(body1, body2)" it works just fine, but I can't get the collision to work with the layer in my tilemap. Can someone please help me?

This is my code (I'll also upload the whole project if someone wants to test it):

/* Goof Troop fan game */

let width = 1152, height = 648;
let transparent = false, antialias = false;

let player_speed = 200;
let scale_factor = 3;

let cursors;
let goofy;
let star_blocks;

let map;
let layer1, layer2;

let game = new Phaser.Game(width, height, Phaser.AUTO, "", {
    preload: preload,
    create: create,
    update: update,
    render: render
}, transparent, antialias);

/* Load the assets */
function preload() {
    game.load.image("star-block", "assets/star-block.png"); // 16 x 16
    game.load.image("tileset-stage-1", "assets/tileset-stage-1.png");
    game.load.spritesheet("goofy", "assets/goofy.png", 17, 39);
    game.load.audio("to-the-south", "assets/to-the-south.ogg");
    game.load.tilemap("stage-1", "assets/stage-1.json", null, Phaser.Tilemap.TILED_JSON);

    game.time.advancedTiming = true;    // Required to check FPS
}

function render() {
    game.debug.text("FPS: " + game.time.fps, 10, 20, "white");
    game.debug.body(goofy); // Show player body
}

function create() {
    /* Set fullscreen mode */
    game.scale.fullScreenScaleMode = Phaser.ScaleManager.SHOW_ALL;
    game.scale.pageAlignHorizontally = true;

    /* Switch between fullscreen and windowed when clicked */
    game.input.onDown.add(fullscreen, this);

    /* Loop background music */
    let bg_music = game.add.audio("to-the-south");
    bg_music.loopFull();

    map = game.add.tilemap("stage-1");
    map.addTilesetImage("tileset-stage-1");
    layer1 = map.createLayer("Background");
    layer2 = map.createLayer("Immovable objects");

    layer1.scale.set(scale_factor);
    layer2.scale.set(scale_factor);

    layer1.resizeWorld();
    layer2.resizeWorld();

    /* Add collision to the layer and debug to check it's working */
    map.setCollisionBetween(1, 1000, true, layer2);
    layer2.debug = true;

    /* Enable Arcade physics engine */
    game.physics.startSystem(Phaser.Physics.ARCADE);

    star_blocks = game.add.group();
    star_blocks.enableBody = true;

    /* Stage objects */
    let star_block_1 = star_blocks.create(450, 245, "star-block");
    star_block_1.scale.setTo(scale_factor);

    star_blocks.setAll("body.collideWorldBounds", true);
    star_blocks.setAll("body.mass", 0.05);  // So the star blocks will move faster when kicked

    goofy = game.add.sprite(320, 160, "goofy"); // 16 x 20
    goofy.scale.setTo(scale_factor);

    goofy.animations.add("up", [2], 60, false);
    goofy.animations.add("down", [1], 60, false);
    goofy.animations.add("left", [3], 60, false);
    goofy.animations.add("right", [4], 60, false);


    /* Enable physics for Goofy */
    game.physics.arcade.enable(goofy);

    goofy.body.collideWorldBounds = true;
    goofy.body.setSize(17, 8, 0, 31);

    cursors = game.input.keyboard.createCursorKeys();
}

function fullscreen() {
    if(game.scale.isFullScreen) {
        game.scale.stopFullScreen();
    } else {
        game.scale.startFullScreen();
    }
}

function update() {
    /* This works fine */
    game.physics.arcade.collide(goofy, star_blocks);

    /* This doesn't seem to work */
    game.physics.arcade.collide(goofy, layer2);
    game.physics.arcade.collide(star_blocks, layer2);

    /* Reset Goofy's velocity */
    goofy.body.velocity.x = 0;
    goofy.body.velocity.y = 0;

    if(cursors.up.isDown) {
        goofy.body.velocity.y = -player_speed;
        goofy.animations.play("up");
    } else if(cursors.down.isDown) {
        goofy.body.velocity.y = +player_speed;
        goofy.animations.play("down");
    }

    if(cursors.left.isDown) {
        goofy.body.velocity.x = -player_speed;
        goofy.animations.play("left");
    } else if(cursors.right.isDown) {
        goofy.body.velocity.x = +player_speed;
        goofy.animations.play("right");
    }
}

 

GoofTroop.7z

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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