bobonthenet Posted April 22, 2017 Share Posted April 22, 2017 I am really struggling with collision on CSV tilemaps. I am creating the CSV string in the create function before doing the Phaser tilemap stuff. The map is rendering as expected along with the player sprite but collisions are not happening. The CSV map consists currently of 1s and 0s but I'll add more later. 0 should be impassable. Here is what I've got in the create function. this.game.cache.addTilemap('map', null, csvContent, Phaser.Tilemap.CSV); this.map = this.game.add.tilemap('map', 16, 16); this.map.addTilesetImage('dungeontile'); this.map.setCollision(0); //I don't think this is doing what I think it does. this.layer = this.map.createLayer(0); this.layer.resizeWorld(); In the update function, I have this: this.game.physics.arcade.collide(this.player, this.layer); I think that is the important code. I also have arcade physics enabled for the game and player. Sorry if I'm missing something. Any thoughts on why this isn't working? Link to comment Share on other sites More sharing options...
pcholt Posted April 23, 2017 Share Posted April 23, 2017 this.map.setCollision(0); I think this is telling the map to set up a collision system where tile value 0 is the solid tile, not the empty tile. perhaps you mean this.map.setCollisionByExclusion([0]); http://phaser.io/docs/2.4.4/Phaser.Tilemap.html#setCollisionByExclusion Link to comment Share on other sites More sharing options...
bobonthenet Posted April 24, 2017 Author Share Posted April 24, 2017 @pcholt, thanks but that isn't working either. My thinking was that the INT values given to those methods correspond to the values in the CSV right? So in my CSV if position [x, y] is 0 and I have this.map.setCollision(0) then position [x, y] should be impassable. Is that right? For a bit more context I have attached a screen shot of the game. I'm just testing randomly generated maps so I've kept this as simple as I can. I'm using two images one for the map tile which is just a 16x16 square and one for the dungeoneer/player which is also a 16x16 square. I've resized the dungeoneer in game using this.player.scale.setTo(.5). Everywhere in the map that you see the dungeon image tile is a 0 on the CSV. Those should not be passable by the player. I'm using arcade physics and I have it enabled for the player sprite. this.game.physics.enable(this.player, Phaser.Physics.ARCADE); In the update function I have this.game.physics.arcade.collide(this.player, this.layer); I feel like these are all as I would want them to be and that I must be missing something else that needs to be in my code. In the future, I'd like to try and generate more complicated maps that consist of more than ones and zeroes and perhaps even use multiple layers but I feel like first I really need to figure out how collision works. In the past, I've hand drawn maps using Tiled Editor and exported to JSON and never had problems. I'm at a complete loss here though with using randomly generated maps as CSV. What am I missing? Drag files here to attach, or choose files... Max total size 4.88MB Insert other media Uploaded Images Link to comment Share on other sites More sharing options...
samme Posted April 24, 2017 Share Posted April 24, 2017 I think you can setCollision only after createLayer. Link to comment Share on other sites More sharing options...
bobonthenet Posted April 24, 2017 Author Share Posted April 24, 2017 55 minutes ago, samme said: I think you can setCollision only after createLayer. I moved it and the player is still walking through walls. Link to comment Share on other sites More sharing options...
samme Posted April 24, 2017 Share Posted April 24, 2017 Link to comment Share on other sites More sharing options...
bobonthenet Posted April 24, 2017 Author Share Posted April 24, 2017 Oh man, I figured it out. I'm going to make sure I get a really good nights sleep before I get back to this project again. The map generation I was doing was fine. I saw this when I looked at the codepen. What I was doing wrong had to do with the player movement. Instead of changing the velocity I was actually changing the player position. I struggled with this for 2 days because I didn't even look at the movement code! I guess my lesson here is not to rule anything out before actually looking at the code. Link to comment Share on other sites More sharing options...
Recommended Posts