Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

how i can collider a tilemap and sprite correctly?


jopcode
 Share

Recommended Posts

Hello, i have a map using tilemap and a player, both with collision, but when i press UP key for jump the player can jump everywhere meanwhile him have contact with a part of a platform ( down, top, left, right), i am trying used "body.touching" but it never return me true in any property (up,down,left,right).

Similarly sometimes player(sprite) can cross a platform when both(player and platform) have collision. Any idea?

(sorry for my bad english)

My code:

// Only 1 game state
var GameState = {
  preload: function() {
    this.game.load.image('sky', 'assets/sky.png');
    this.game.load.image('star', 'assets/star.png');
    this.game.load.spritesheet('player', 'assets/player.png', 70, 94)
    this.game.load.tilemap('tilemap', 'assets/tileset.json', null, Phaser.Tilemap.TILED_JSON);
    this.game.load.image('tiles', 'assets/ground_tileset.png')
  },

  create: function() {
    this.game.physics.startSystem(Phaser.Physics.ARCADE);

    this.background = this.game.add.sprite(0, 0, 'sky');
    this.background.fixedToCamera = true ;
    this.map = this.game.add.tilemap('tilemap');
    this.map.addTilesetImage('ground_tileset', 'tiles');

    this.groundLayer = this.map.createLayer('GroundLayer');
    this.groundLayer.enableBody = true;
    this.map.setCollisionBetween(0, 600);
    this.player = this.game.add.sprite(0, 400, 'player');
    this.game.physics.arcade.enable(this.player);
    this.player.enableBody = true;
    this.groundLayer.resizeWorld();

    this.player.body.bounce.y = 0.2;
    this.player.body.gravity.y = 300;
    this.player.body.collideWorldBounds = true;

    this.player.animations.add('right', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10], 10, true);
    //this.player.animations.play('right');
    this.game.camera.follow(this.player);
    this.cursors = this.game.input.keyboard.createCursorKeys();
  },

  update: function() {
    this.hitPlatform = this.game.physics.arcade.collide(this.player, this.groundLayer);
    if(this.cursors.right.isDown) {
      console.log(this.player.body.wasTouching);
      this.player.body.velocity.x = 300;
    } else if(this.cursors.left.isDown) {
      this.player.body.velocity.x = -300;
    } else {
      this.player.body.velocity.x = 0;
    }
    if(this.cursors.up.isDown && this.hitPlatform) {
      this.player.body.velocity.y = -350;
      console.log(this.player.body.touching);
    }
  },
};

// Initialize Phaser
var game = new Phaser.Game(800, 600, Phaser.AUTO);
game.state.add('GameState', GameState);
game.state.start('GameState');

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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