Jump to content

Can't collide sprite and tilemap


Recommended Posts

So, I have trouble with this every project I start, but this time I really can't put my finger on it. Basically, it's the same old "sprite doesn't collide with tilemap", but look...

let stage = game.add.tilemap("test-map");
let foreground = stage.createLayer("foreground");

let player = game.add.sprite(x,y,"key","frame"); // Don't worry, "x" and "y" are above the tiles and sprite gracefully falls through the tiles due to gravity
player.body.gravity.y = 100;
player.body.collideWorldBounds = true;

game.physics.arcade.collide(player, foreground);


Both sprite and tilemap have collision, as you can see. They are set to collide in the last line. The sprite even collides with the world bounds, but not with the tilemap. What could possibly be wrong with my setup?

Edited by QLNEO
Fixed foreground variable name in .collide()
Link to comment
Share on other sites

I remeber something like this that i've made for a phaser version of Zelda

var PlayState = function() {};

PlayState.prototype = {
	init: function() {

		// Game stats for local storage, rewards and win/loss screen
		this.gameStats = {
			health : 3


  create: function() {

		    map = game.add.tilemap('hyrule');
		    map.addTilesetImage('light_world.tiles', 'light_world');

		// Order of layer in tile editor is important for collision
		    layer1 = map.createLayer('Calque de Tile 1');
		    layer2 = map.createLayer('Calque 2');


		//The big tree i didn't uses a collision for the little trees yet
		    map.setCollisionBetween(1078, 1085, true, layer2);//look at the id in tiled on the spritesheet
		    map.setCollisionBetween(1174, 1181, true, layer2);
		    map.setCollisionBetween(1271, 1276, true, layer2);
		    map.setCollisionBetween(1369, 1370, true, layer2);

				player = new Player();

		//As the order of the layers is important it's also important here
		//to call it after the sprite
		    layer3 = map.createLayer('Calque 3');


	update: function(){
		game.physics.arcade.collide(player, layer2);
© 2017 GitHub, Inc.


Link to comment
Share on other sites

How so? But nevertheless, I define my player after the collision layer, which is exactly what you did. My game currently has only that layer (and an object layer, that isn't created because it's an object layer and just stores data)

Link to comment
Share on other sites


  • Recently Browsing   0 members

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