Jump to content

Can't collide sprite and tilemap


QLNEO
 Share

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");
stage.addTilesetImage("test-tileset","test-tileset",16,16);
let foreground = stage.createLayer("foreground");
stage.setLayer("foreground");
foreground.resizeWorld();
stage.setCollisionByExclusion([]);

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
game.physics.arcade.enable(player);
player.body.gravity.y = 100;
player.body.collideWorldBounds = true;

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

67TZGfJ.png

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

		//World
		    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');

		    layer1.resizeWorld();

		//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);

		//Playerand
				player = new Player();
				player.create();

		//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);
		player.update();
	}
}
© 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

 Share

  • Recently Browsing   0 members

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