Sign in to follow this  

Collision between tilemap layer and player not working

Recommended Posts

Hello everyone,

first time here (so might excuse if its the wrong section) but I'm fricking desperate at the moment.

I'm trying for about 2 days now to get my player to collide with my "Obstacle" tilemap layer. I looked at almost every post about this here or elsewhere but I can't get it to work. It might be a simple thing but I can't find whats wrong and I want to throw my pc out of the window now. So maybe one of you guys have an idea and can help me. Any help is appreciated.

The code is the Following:


var game = new Phaser.Game(255, 255, Phaser.AUTO, '', { preload: preload,
create: create, 
update: update 

var player;
var speed = 2;
var floor;
var walls;
var obsts;
var map;

function preload() 
  game.load.spritesheet('player','assets/player_spritesheet.png', 48,48);

  game.load.image("tileset", "assets/tileset.png");
  // Phaser.Canvas.setSmoothingEnabled(, false); = Phaser.ScaleManager.USER_SCALE;, 3); = true;

function create() 
  // Create map
  map = game.add.tilemap("map", 48, 48, 16, 16);

  // Use layers
  floor = map.createLayer('Floor');
  walls = map.createLayer('Walls');
  obsts = map.createLayer('Obstacles');

  // create player
  player = game.add.sprite(50,50,'player');

  // all physics related stuff

  game.physics.enable(player, Phaser.Physics.ARCADE);

  player.body.setSize(10, 14, 2, 1);

  player.body.collideWorldBounds = true;

  // walk animations
  player.animations.add('walk_right', [12,13,14,15],10,true);
  player.animations.add('walk_left', [20,21,22,23],10,true);
  player.animations.add('walk_down', [8,9,10,11],10,true);
  player.animations.add('walk_up', [16,17,18,19],10,true);

  //attack animations
  player.animations.add('attack_down', [24,25,26,27],10,true);
  player.animations.add('attack_right', [28,29,30,31],10,true);
  player.animations.add('attack_left', [32,33,34,35],10,true);
  player.animations.add('attack_up', [36,37,38,39],10,true);'idle');;

function update() 
  // Walk mechanic
    player.x -= speed;
  else if (game.input.keyboard.isDown(Phaser.Keyboard.D))
    player.x += speed;
    player.y -= speed;
  else if (game.input.keyboard.isDown(Phaser.Keyboard.S))
    player.y += speed;

  // attack mechanic
  if (game.input.keyboard.isDown(Phaser.Keyboard.LEFT))
  if (game.input.keyboard.isDown(Phaser.Keyboard.RIGHT))
  if (game.input.keyboard.isDown(Phaser.Keyboard.UP))
  if (game.input.keyboard.isDown(Phaser.Keyboard.DOWN))
  //jump back to idle when not walking or attacking
  else if (
    !game.input.keyboard.isDown(Phaser.Keyboard.A) &&   !game.input.keyboard.isDown(Phaser.Keyboard.D) &&   !game.input.keyboard.isDown(Phaser.Keyboard.W) &&   !game.input.keyboard.isDown(Phaser.Keyboard.S) &&
    !game.input.keyboard.isDown(Phaser.Keyboard.UP) &&   !game.input.keyboard.isDown(Phaser.Keyboard.DOWN) &&   !game.input.keyboard.isDown(Phaser.Keyboard.LEFT) &&   !game.input.keyboard.isDown(Phaser.Keyboard.RIGHT)){'idle');

 Also if you see any other mistakes, feel free to send suggestions to this as well.


Thank you in advance.


Kind regards

Share this post

Link to post
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.

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.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.