Little help with my tilemap collision?

Recommended Posts

Hey guys,


I try to figure these things on my own but I'm on day 3 of troubleshooting these tilemaps.

Here's the code. Essentially, the player isn't colliding with with the collidable tiles of the tilemap but it looks like my jumperBot sprite is but with all tiles, not just the set collision tiles.

If you can help me figure this out, I will grant you a 1000 points!


function preload() {game.load.image('player', 'assets/player.png');game.load.spritesheet('jumperBot', 'assets/jumper-bot.png', 48, 98);game.load.tilemap('level1', 'assets/cave_tilemap.json', null, Phaser.Tilemap.TILED_JSON);game.load.tileset('tiles', 'assets/cave_tiles.png', 32, 39);}function create() {// TILEMAPmap = game.add.tilemap('level1');tileset = game.add.tileset('tiles');tileset.setCollisionRange(1, 6, true, true, true, true);tileset.setCollision(7, false, false, false, false);layer = game.add.tilemapLayer(0,0,800,600,tileset,map,0);// JUMPERBOTjumperBot = game.add.sprite((game.world.width - 275), (game.world.height - 250), 'jumperBot');jumperBot.body.collideWorldBounds = true;jumperBot.body.gravity.y = 6;jumperBot.animations.add('jump', [1,2,0], 12, false);jumperBot.animations.add('idle', [1], 10, false);}function update() {game.physics.collide(jumperBot, layer);game.physics.collide(player, layer);}


Share this post

Link to post
Share on other sites

Disclaimer: I'm still new to phaser and this might not bet the ultimate answer. ;)


The tileset index begins at 0 and ends 6 if you have 7 tiles in your tileset (total number of tiles -1)


So it would be:

    tileset.setCollisionRange(0, tileset.total - 1, true, true, true, true);    tileset.setCollision(6, false, false, false, false);
(Set everything to colide "true" and just the last tile in the set to "false")



What you need to make the collision work is velocity.

At the moment you are forcing the player to update it's x/y position instead of giving him a velocity each update and let the physic engine do the work.


What you want to do is settings the players gravity to 0 and give him a velocity if the controls are pressed. 


function update() {	game.physics.collide(jumperBot, layer);	game.physics.collide(player, layer);		// reset the velocity each tick	player.body.velocity.y = 0;	player.body.velocity.x = 0;	if ( cursors.up.isDown ) {		player.body.velocity.y = - 100;	}
and so on :)

It's strange that I cant call the render function after I added it to your game tough :/


Setting Phaser.AUTO to Phaser.CANVAS does the trick with debug informations :)

Edited by Pixelguy

Share this post

Link to post
Share on other sites

Beginner in Phaser here. It also seems you want to test the collision with the background since, you set the background of your level as one of the tiles(6 one I believe) from the tileset your are trying to collide against. You could make a new layer in your Level editor program and fill it with the tiles you want for the background and then set them as non-collide able. I'm new so this might not be the best solution, but it worked for me. 

Share this post

Link to post
Share on other sites

Pixelguy just achieved 1000 points!


Thank you, kind sir. It's working :)

Although if I set gravity to any value, my player disappears for some odd reason. I'll have to do some investigation but otherwise it's totally working. It was the velocity issue. 


@RaptorZen64, that's a good idea too. In fact, that gives me some ideas for the future actually. 

Share this post

Link to post
Share on other sites

re issue with gravity: I see that it is correct in the code you posted, but make sure you are setting 'gravity.y' (or 'x' if you want sideways gravity) and not just 'gravity'. I've missed this before and it causes problems (like sprites not being drawn)

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.