Jump to content

Tilemap sprite collision with layer


xzereus
 Share

Recommended Posts

I'm actually using the 1.2 branch, so that may or may not apply here. So I feel kind of stupid posting here, but I can't seem to figure out exactly how the collision is supposed to be used. I have a top-down (pokemon-style) game with the ground in one layer and the "walls" (trees) in another. Basically, I just want to prevent movement into the trees. Here's essentially what I have: 

// Preloading left out for brevityfunction create() {   sprite = game.add.sprite(160, 160, 'player');   map = game.add.tilemap('testmap');   map.addTilesetImage('forest_tileset-32x32', 'darkforesttiles');   map.createLayer('ground');   layer = map.createLayer('trees');}function update() {   game.physics.collide(sprite, layer, function(s, t) {           // do stuff        }, null, this);   // Control stuff left out for brevity}

I placed a break point in the "// do stuff" area, but it never gets hit. I can move freely past the obstacles in the layer. 

 

I'm clearly doing something wrong, so what is it?

 

Link to comment
Share on other sites

How is the player moved? Are you moving with velocity or moving a set amount of x specified?

Also collisions in tilemaps as standard are

Game.physics.collide(player, layer);

Also might be worth noting you need to set map collisions with tile index. So if you look at the JSON and get the tile I.D. for the tree tile then set the collision like:

map.setCollision(12);

// assuming the tile for tree has the I.D. Of 12.

Link to comment
Share on other sites

  • 2 months later...

How is the player moved? Are you moving with velocity or moving a set amount of x specified?

Also collisions in tilemaps as standard are

Game.physics.collide(player, layer);

Also might be worth noting you need to set map collisions with tile index. So if you look at the JSON and get the tile I.D. for the tree tile then set the collision like:

map.setCollision(12);

// assuming the tile for tree has the I.D. Of 12.

 

What if you are moving with a set amount of x specified?

 

My tiles are 32x32. When I move by velocity, collisions work as expected. When I do an absolute change in position (like with player.body.velocity += 32), my player walks over walls all willy nilly.

 

When I change position by a factor of 32 (like 16 or 2), collisions again work as expected. But I don't want my sprite to be able to move half-tile widths.

 

Any ideas?

Link to comment
Share on other sites

You don't need += with velocity. Its either just a positive or negative.

Velocity.x = 32;

Velocity.x = -32;

As for moving a full tile with velocity per tile, use tweens. You can specify velocity for collisions and move per tile.

That or use velocity and have the player walk as normal but have collision layers in place to stop glitching or map leaving.

Link to comment
Share on other sites

So sorry, I meant 

 

player.body.position.x += 32

 

Late nights are getting me confused. :)

 

This is what's causing me problems. When the player moves in absolute increments, instead of stopping and colliding with a tile, the player will move directly on to the tile when an arrow key is pressed. This only happens when the player moves the exact width of the tile, but when I set player.body.position.x += [a factor of 32], he will stop upon colliding with the tile.

Link to comment
Share on other sites

  • 6 months later...

I have tried every thing possible to get my collision working but i cant i have 4 layer, one of which i intend to collide with the player but the payers seams to walk over every thing..

 

here is s snippet from my map JSON

 

{
         "data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 24, 24, 2678, 24, 24, 24, 24, 23, 0, 2973, 2974, 3073, 3075, 3076, 3075, 3076, 3075, 3076, 3075, 3076, 3074, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 24, 24, 2778, 2678, 223, 223, 224, 23, 2678, 3073, 3074, 2973, 2975, 2976, 2975, 2976, 2975, 2976, 2975, 2976, 2974, 2973, 2974, 0, 0, 2973, 2974, 0, 0, 2973, 2974, 0, 3073, 3074, 0, 0, 2973, 2974, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 223, 24, 23, 2778, 2973, 2974, 0, 0, 2778, 0, 0, 3073, 3075, 3076, 3075, 3076, 3075, 3076, 3075, 3076, 3074, 3073, 3074, 0, 0, 3073, 3074, 0, 0, 3073, 3074, 0, 2973, 2974, 0, 0, 3073, 3074, 0, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 120, 0, 0, 0, 0, 223, 224, 3073, 3074, 0, 0, 2973, 2974, 0, 2973, 2975, 2976, 2975, 2976, 2975, 2976, 2975, 2976, 2974, 0, 2973, 2974, 0, 0, 2973, 2974, 0, 0, 0, 2476, 3073, 3074, 2973, 2974, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1374, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 3073, 3075, 3076, 3075, 3076, 3075, 3076, 3075, 3076, 3074, 0, 3073, 3074, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1374, 4389, 0, 4389, 3074, 0, 0, 0, 0, 0, 0, 2779, 2678, 0, 0, 2973, 2975, 2976, 2975, 2976, 2975, 2976, 2975, 2976, 2974, 0, 0, 2973, 2974, 0, 0, 2478, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4085, 4086, 0, 0, 0, 0, 0, 1374, 4389, 0, 0, 0, 0, 0, 0, 0, 2779, 158, 158, 0, 2778, 2678, 2678, 3073, 3075, 3076, 3075, 3076, 3075, 3076, 3075, 3076, 3074, 0, 0, 3073, 3074, 0, 0, 0, 0, 2973, 2974, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4085, 4086, 0, 0, 0, 0, 0, 4185, 4186, 0, 0, 0, 0, 1374, 4389, 0, 0, 0, 760, 0, 0, 0, 0, 0, 0, 2973, 2974, 0, 2778, 2778, 2678, 3073, 3074, 3073, 3074, 3073, 3074, 3073, 3074, 0, 0, 0, 0, 0, 0, 2973, 2974, 0, 3073, 3074, 0, 0, 3073, 3074, 0, 0, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4185, 4186, 0, 0, 0, 0, 0, 4285, 4286, 0, 0, 0, 0, 4389, 0, 0, 0, 0, 0, 0, 761, 0, 2581, 2582, 2583, 3073, 3074, 0, 158, 0, 2778, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 2973, 2974, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4285, 4286, 0, 0, 0, 0, 0, 4385, 4386, 0, 0, 1373, 1374, 0, 0, 2081, 0, 0, 0, 2081, 0, 0, 2681, 2682, 2683, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 2973, 2974, 0, 0, 2973, 2974, 3073, 3074, 0, 0, 0, 0, 2973, 2974, 0, 2973, 2974, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4385, 4386, 0, 0, 0, 0, 0, 4485, 4486, 1373, 1374, 4389, 4389, 0, 0, 2181, 0, 0, 0, 2181, 0, 0, 4389, 2782, 4389, 0, 0, 0, 2777, 0, 0, 0, 2973, 2974, 0, 3073, 3074, 0, 0, 3073, 3074, 0, 0, 2973, 2974, 0, 0, 3073, 3074, 0, 3073, 3074, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 4485, 4486, 0, 0, 0, 0, 0, 1373, 1374, 4389, 4389, 0, 0, 0, 0, 0, 0, 0, 0, 3158, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 0, 0, 0, 0, 2973, 2974, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3174, 3274, 0, 0, 0, 0, 0, 2087, 2087, 0, 2780, 0, 0, 0, 0, 0, 2973, 2974, 0, 2481, 2087, 2087, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 3274, 2087, 0, 0, 0, 0, 0, 2087, 2087, 0, 0, 0, 0, 0, 0, 0, 3073, 3074, 0, 0, 2087, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 2381, 0, 0, 0, 2780, 0, 0, 0, 2381, 0, 0, 2780, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
Link to comment
Share on other sites

and here is my code after tying everything humanly possible.

 

function create(){
    
    game.physics.startSystem(Phaser.Physics.ARCADE);
    
    this.map = game.add.tilemap('map');
 
    this.map.addTilesetImage('world-01', 'tiles');
    
    this.map.setCollisionBetween(24, 41);
    this.map.setCollisionBetween(1265);
    
    this.layer = this.map.createLayer('collide-layer');
    this.layer.resizeWorld();
    
    this.p = game.add.sprite(32, 32, 'player');
    
    game.physics.enable(this.p);
    
    this.p.body.collideWorldBounds = true;
 
    game.camera.follow(this.p);
 
    cursors = game.input.keyboard.createCursorKeys();
}
 

Please i have built a lot already with phaser for this to start happening....

:-(

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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