QLNEO Posted November 7, 2017 Share Posted November 7, 2017 (edited) 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); 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 November 7, 2017 by QLNEO Fixed foreground variable name in .collide() Link to comment Share on other sites More sharing options...
Capitaine Herlock Posted November 7, 2017 Share Posted November 7, 2017 It seems you didn't set the collision layer, like you did with foreground right ? Link to comment Share on other sites More sharing options...
QLNEO Posted November 7, 2017 Author Share Posted November 7, 2017 Well, the foreground is the collision layer. I forgot to change the names though, gonna fix that real quick Capitaine Herlock 1 Link to comment Share on other sites More sharing options...
Capitaine Herlock Posted November 7, 2017 Share Posted November 7, 2017 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 More sharing options...
QLNEO Posted November 7, 2017 Author Share Posted November 7, 2017 Looking at your code, I'm still not seeing any reason for mine to fail! Link to comment Share on other sites More sharing options...
Capitaine Herlock Posted November 7, 2017 Share Posted November 7, 2017 I've noticed that the order in which you declare the layers and the player are important Link to comment Share on other sites More sharing options...
QLNEO Posted November 7, 2017 Author Share Posted November 7, 2017 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 More sharing options...
Capitaine Herlock Posted November 7, 2017 Share Posted November 7, 2017 The first layer i use is a green background then i add a tree from Zelda light world spritesheet, right after i define the tiles which the sprite will collide, in this order it worked fine, then i create the player Link to comment Share on other sites More sharing options...
QLNEO Posted November 7, 2017 Author Share Posted November 7, 2017 Well the Discord server saved my butt 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! Capitaine Herlock 1 Link to comment Share on other sites More sharing options...
Recommended Posts