Jump to content

help me pls, tilemaps collision


danteAGAIN
 Share

Recommended Posts

Hello everyone,

I am beginner with phaser, i am French and my english is not very nice. I need your help, i am blocked on a collide function. I want my character to collide with my second layer (tree, montain, statue..). (i use tiled map editor and export in json).

my code :  

function preload:

    game.load.tilemap('map', 'assets/tilemaps/maps/map.json', null, Phaser.Tilemap.TILED_JSON);
    game.load.image('tiles', 'assets/tiles/tiles.png');
    game.load.spritesheet('perso', 'assets/sprite/spritetest.png', 32, 47);

function create : 

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

    map = game.add.tilemap('map');
    map.addTilesetImage('tiles');
    layer = map.createLayer('layer1');
    layer2 = map.createLayer('layer2');

    layer2.resizeWorld();
    map.setCollisionBetween(0,2000);
    //PLAYER
    // ajout player et parametre
    player = game.add.sprite(250, 50, 'perso');


        //  We need to enable physics on the player
    game.physics.arcade.enable(player, Phaser.Physics.ARCADE);

 

function update :

game.physics.arcade.collide(player, layer2);

 

i show an image for understand my problem.

Thx to read my message

collision.png

Link to comment
Share on other sites

I am from Brazil then my english is not very nice too.

How is the player moved? Are you moving with velocity or moving a set amount of x specified?
usually you need use something like this:

 

if (controls.left.isDown) {
player.animations.play('run')
player.scale.setTo(-1, 1)
player.body.velocity.x = -player.walkingSpeed
}
 
another tip is using collision only in the layer that you want:

map.setCollisionBetween(0, 2000, 'layer2')
Link to comment
Share on other sites

On 29/05/2017 at 9:13 PM, [email protected] said:
On 29/05/2017 at 9:13 PM, [email protected] said:

My character can move , the problem is the collision with the layer 2. I have try to use " map.setCollisionBetween(0, 2000, 'layer2') but collision do not work.

just for info, I use this : 

if (cursors.left.isDown)
    {
        //  Move to the left
        player.body.velocity.x = -150;

        player.animations.play('left');
    }

but I think is not my problem because my character can run and stop. I dont know why, i can collide my layer( who is my background) but i cant collide my second layer (tree, montain..)

 

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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