hustlerinc Posted November 7, 2014 Share Posted November 7, 2014 I'm having trouble with collisions between the player sprite and a tilemap layer created with Tiled Map Editor. The layer is drawn on the screen, and if I turn on debug I see the hitboxes highlighted but there's no collisions. Here's the relevant code:var game = new Phaser.Game(480, 320, Phaser.AUTO, '', { preload: preload, create: create, update: update });function preload() { game.load.spritesheet('player', 'assets/characters.png', 16, 16); game.load.image('tiles', 'assets/tiles.png'); game.load.tilemap('map', 'assets/map.json', null, Phaser.Tilemap.TILED_JSON);}function create() { map = game.add.tilemap('map'); map.addTilesetImage('tiles', 'tiles'); bgLayer = map.createLayer('bgLayer'); collisionLayer = map.createLayer('collisionLayer'); // player should collide with anything on this layer map.setCollisionBetween(0, 200, true, 'collisionLayer'); player = game.add.sprite(0, 304, 'player'); // this sprite has to collide with collisionLayer game.physics.arcade.enable(player); player.body.collideWorldBounds = true; collisionLayer.debug = true;}function update() { game.physics.arcade.collide(player, collisionLayer); playerMove();}I'm sure I'm doing something wrong, but I don't know what. Link to comment Share on other sites More sharing options...
SoulBeaver Posted November 9, 2014 Share Posted November 9, 2014 Hello, I'm currently working on a tilemap as well, so I'll try to be of some assistance if possible. First, you're not resizing the map to fit the backgroundLayer, which you probably should do.bglayer.resizeWorld();Second, make sure you enable the physics system at the start of the game:game.physics.startSystem(Phaser.Physics.ARCADE);I'm not sure if this step is necessary, but I specified the body size for my player spriteplayer.body.setSize(24, 24);Let me know if any of those steps help in resolving the issue. If not, we can try digging a little deeper. Link to comment Share on other sites More sharing options...
hustlerinc Posted November 10, 2014 Author Share Posted November 10, 2014 Hello, I'm currently working on a tilemap as well, so I'll try to be of some assistance if possible. First, you're not resizing the map to fit the backgroundLayer, which you probably should do.bglayer.resizeWorld();Second, make sure you enable the physics system at the start of the game:game.physics.startSystem(Phaser.Physics.ARCADE);I'm not sure if this step is necessary, but I specified the body size for my player spriteplayer.body.setSize(24, 24);Let me know if any of those steps help in resolving the issue. If not, we can try digging a little deeper.Hi, thanks for helping me. I'm resizing the map, just didn't want to confuse with too much code in the post. Anyway I addedgame.physics.startSystem(Phaser.Physics.ARCADE);to my create function right before the call to map.setCollisionBetween(); and I addedplayer.body.setSize(16, 16);after the line enabling arcade physics on the player (also in the create function). Same issue though, I can walk through the tiles I'm supposed to collide with. If it helps here are the tutorials I've read, and I've done pretty much what they do only different ways of setting up the preload, create and update functions.http://svejkgames.com/blog/post/using-tilemaps-in-phaser-framework/http://www.gamedevacademy.org/html5-phaser-tutorial-top-down-games-with-tiled/Do you have any other ideas why it doesn't work for me? Link to comment Share on other sites More sharing options...
hustlerinc Posted November 10, 2014 Author Share Posted November 10, 2014 Okay, I figured it out. The problem was in the playerMove() funcion, changing the movement from exact pixels toplayer.body.velocity.y -= 50;solved it. Seems like a no-brainer now that I think of it. Link to comment Share on other sites More sharing options...
Recommended Posts