Jump to content


Photo
- - - - -

Little help with my tilemap collision?

tilemap collision

  • Please log in to reply
8 replies to this topic

#1 eloguvnah

eloguvnah

    Member

  • Members
  • PipPip
  • 22 posts

Posted 09 January 2014 - 04:37 AM

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() {


// TILEMAP
map = 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);


// JUMPERBOT
jumperBot = 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);


}

 



#2 4ucai

4ucai

    Advanced Member

  • Members
  • PipPipPip
  • 48 posts

Posted 09 January 2014 - 06:53 AM

Hope rich could release the 1.1.4. There might still unfinished or bugs in the dev branch.



#3 Pixelguy

Pixelguy

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts
  • LocationGermany

Posted 09 January 2014 - 12:06 PM

Could you upload your project with all files somewhere?



#4 eloguvnah

eloguvnah

    Member

  • Members
  • PipPip
  • 22 posts

Posted 09 January 2014 - 03:47 PM

Sure!

 

https://www.dropbox....yq/upgrader.zip



#5 Pixelguy

Pixelguy

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts
  • LocationGermany

Posted 09 January 2014 - 06:47 PM

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 :/

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

Edited by Pixelguy, 09 January 2014 - 06:56 PM.


#6 RaptorZen64

RaptorZen64

    Newbie

  • Members
  • Pip
  • 4 posts

Posted 09 January 2014 - 07:02 PM

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. 



#7 eloguvnah

eloguvnah

    Member

  • Members
  • PipPip
  • 22 posts

Posted 09 January 2014 - 08:19 PM

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. 



#8 Pixelguy

Pixelguy

    Advanced Member

  • Members
  • PipPipPip
  • 39 posts
  • LocationGermany

Posted 09 January 2014 - 08:38 PM

Glad I could help! :)

If you can't figure this out by yourself feel free to share the project again.

#9 jcs

jcs

    Advanced Member

  • Members
  • PipPipPip
  • 201 posts
  • Locationusa

Posted 11 January 2014 - 01:27 AM

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)







Also tagged with one or more of these keywords: tilemap, collision

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users