Sign in to follow this  

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

Share this post

Link to post
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.


Share this post

Link to post
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)

Share this post

Link to post
Share on other sites

Well the Discord server saved my butt :lol: turns out there was something you did there that I forgot to do.

Namely, put the .collide() on the update function. Problem solved, and I have to seriously write that up somewhere so I never ever forget that again.

Thanks for the assistence though!

Share this post

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.