Jump to content

border collision with body.touching and tilemap problem


ghostrifle
 Share

Recommended Posts

Hi guys,

 

I have a strange problem.  I've created a tilemap and player sprite. Collision between them works, but I need to know on which side of the player the colission happend. I've llocked through the examples and the docs and found .body.touching.X. So, this was exactly what I was looking for. But it doesn't work. Down,up,left and right are always false. Even in the moment when the player sprite touches a collision tile. The variable "none" of body.touching is always true.

 

Any help would be great!

 

Here's some code I'm using:

 function create() {      // setting global gravity to 100      game.physics.gravity.y = 100;      cursors = game.input.keyboard.createCursorKeys();      // adding the tilemap created with Tiled      map = game.add.tilemap('current_level');      map.addTilesetImage('tiles', 'tiles');      // the gray tiles shall be responsible for collisions, check the tiles.png, the first one is the gray one      map.setCollisionBetween(0, 1);      // loading the layer from the tilemap, the layer name got defines in the Tiled editor      map_layer = map.createLayer('Level');      map_layer.resizeWorld();      map_layer.debug = true; // show the bounding boxes for collisions      // creating and adding the player sprite      player = game.add.sprite(50,400,"player");      player.body.collideWorldBounds = true; // don't move out of the screen      player.body.immovable = true; // no bouncing or other fancy physics stuff for the player      player.body.linearDamping = 0;      // the button to change the gravity      button = game.add.button(250,400, 'gravity_button', action_flip_gravity, this, 2, 1, 0);      button.body.allowGravity = false;    } function update(){      // update collision checks for the player an the map every update cycle      game.physics.collide(player, map_layer,collide_player_with_world);}function collide_player_with_world(player,map_layer){      console.log("collide_player_with_world none: " + player.body.touching.none);      if(!player.body.touching.down && game.physics.gravity.y > 0){        console.log("collide_player_with_world down: " + player.body.touching.down);        move_to_x = null;        move_direction = 0;        player.body.velocity.setTo(0,0);      }    }

iie5mrm6.png

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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