Jump to content

CSV Tilemap Collisions not Working


bobonthenet
 Share

Recommended Posts

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

@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?

 

Uploaded Images

  •  
  •  

randommap.PNG

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

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