Jump to content

Big problem with tiledmap collision


bloudman
 Share

Recommended Posts

Hello everyone ! 

I'm actually working with phaser for a project of my school ! 

I'm trying to show a small map created with tiled and to collide with some elements.

I have followed phaser example on this but nothing happened :/ 

My map is correctly created (on the screen also) but there is no collision ^^

I've spent 2 hours or more to fix this and ... it's a fail for the moment :/ 

 

If someone can help me to understand my problem it's could be very helpfull :/ 

(sorry for my poor english)

that's my code and I sent all files in the zip attached bellow

const width = 1280;
const height = 720;

// Phaser
var game = new Phaser.Game(width, height, Phaser.AUTO, 'game', { preload: preload, create: create, update: update});
var voiture;
var map;
var layer;
var layer1;



var upKey;
var downKey;
var leftKey;
var rightKey;


function preload () {

    game.load.image('car', 'img/car.png');

    game.load.tilemap('mapTest', 'map.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', 'img/tileset.png');


}

function create() {

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

    game.stage.backgroundColor = '#fffff0';
    map = game.add.tilemap('mapTest');
    map.addTilesetImage('tileSet', 'tiles', 16, 16, 0); 

    console.log(map);
    layer = map.createLayer("background");
    layer1 = map.createLayer("collide");
    layer1.debug = true;
    //layer.resizeWorld();


    voiture = game.add.sprite(game.world.centerX - 50, game.world.centerY, 'car');
    voiture2 = game.add.sprite(game.world.centerX, game.world.centerY, 'car');
    game.physics.enable(voiture, Phaser.Physics.ARCADE);
    game.physics.enable(voiture2, Phaser.Physics.ARCADE);
    voiture.body.collideWorldBounds = true;
    voiture2.body.collideWorldBounds = true;
    voiture2.body.immovable = true;

    
    upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
    downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
    
    voiture.body.velocity.x = 10;

    map.setCollisionBetween(1, 2);


}


function update() {
    if (upKey.isDown) {
        voiture.body.y--;
    }
    else if (downKey.isDown) {
        voiture.body.y++;
    }

    if (leftKey.isDown) {
        voiture.body.x--;
    }
    else if (rightKey.isDown) {
        voiture.body.x++;
    }


    game.physics.arcade.collide(voiture, voiture2);
    game.physics.arcade.collide(voiture, layer1);

    game.debug.bodyInfo(voiture, 16, 24);


}

 

phaser2.zip

phaser2.zip

Link to comment
Share on other sites

No problem :) 

 

done:

const width = 1280;
const height = 720;

// Phaser
var game = new Phaser.Game(width, height, Phaser.AUTO, 'game', { preload: preload, create: create, update: update});
var voiture;
var map;
var layer;
var layer1;



var upKey;
var downKey;
var leftKey;
var rightKey;


function preload () {

    game.load.image('car', 'img/car.png');

    game.load.tilemap('mapTest', 'map.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', 'img/tileset.png');


}

function create() {

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

    game.stage.backgroundColor = '#fffff0';
    map = game.add.tilemap('mapTest');
    map.addTilesetImage('tileSet', 'tiles', 16, 16, 0); 
    map.setCollisionBetween(1, 2, true, layer1);

    console.log(map.collision);
    layer = map.createLayer("background");
    layer1 = map.createLayer("collide");
    layer1.debug = true;
    //layer.resizeWorld();


    voiture = game.add.sprite(game.world.centerX - 50, game.world.centerY, 'car');
    voiture2 = game.add.sprite(game.world.centerX, game.world.centerY, 'car');
    game.physics.enable(voiture, Phaser.Physics.ARCADE);
    game.physics.enable(voiture2, Phaser.Physics.ARCADE);

    game.physics.enable(layer1, Phaser.Physics.ARCADE, true);

    voiture.body.collideWorldBounds = true;
    voiture2.body.collideWorldBounds = true;
    voiture2.body.immovable = true;

    
    upKey = game.input.keyboard.addKey(Phaser.Keyboard.UP);
    downKey = game.input.keyboard.addKey(Phaser.Keyboard.DOWN);
    leftKey = game.input.keyboard.addKey(Phaser.Keyboard.LEFT);
    rightKey = game.input.keyboard.addKey(Phaser.Keyboard.RIGHT);
    
    voiture.body.velocity.x = 10;



}


function update() {
    if (upKey.isDown) {
        voiture.body.y-=2;
    }
    else if (downKey.isDown) {
        voiture.body.y+=2;
    }

    if (leftKey.isDown) {
        voiture.body.x-=2;
    }
    else if (rightKey.isDown) {
        voiture.body.x+=2;
    }


    game.physics.arcade.collide(voiture, voiture2);
    game.physics.arcade.collide(voiture, layer1);

    game.debug.bodyInfo(voiture, 16, 24);


}

but it is still not working :/ 

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.

Guest
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.

Loading...
 Share

  • Recently Browsing   0 members

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