Jump to content

Changing Map in Phaser


xzereus
 Share

Recommended Posts

I'm currently having some issues getting generated maps to work. I've built a generator that creates a 2D array and stores all of the gids in this array. I've confirmed this works by converting the array to a 1D array and pasting the result into a Tiled map and loading it up in Tiled. It all looks good.

 

The problem comes when I try to switch to the map from the current map in game. I've been having trouble finding info on switching maps, so I just threw together some code. Here's what I have that is most definitely NOT working:

// destroying old mapthis.map.destroy();// This is the 2D array in the form generatedMap[x][y] which was createdvar generatedMap = generateMap();var tilemap = new Phaser.Tilemap(game, null, tile_size, tile_size, mapWidth, mapHeight);tilemap.addTilesetImage('tileset');// The floor tile has gid 1, so I'm putting this in every positionvar floor = tilemap.createBlankLayer('floor', mapWidth, mapHeight, tile_size, tile_size);for(var x=1; x<tilemap.width-1; x++)     for(var y=1; y<tilemap.height-1; y++)        tilemap.putTile(1, x, y, 'floor');// grab the gids from the generatedMap and put them in the walls layer		var walls = tilemap.createBlankLayer('walls', mapWidth, mapHeight, tile_size, tile_size);for(var x=0; x<tilemap.width; x++)     for(var y=0; y<tilemap.height; y++)        tilemap.putTile(generatedMap[x][y], x, y, 'walls');tilemap.setCollisionBetween(1, 20, true, walls);

This does load up something, but it's not my map. It seems to be a random mixture of the tiles in the tileset with big holes in between. Not only that, but my entire HUD and my player sprite disappear beneath the newly generated tiles. I'm surprised that switching maps is not as straightforward as starting the first one, but I need to figure out the best way to do this. Please let me know what I'm doing wrong here or if I can provide any more information.

 

Thanks!

Link to comment
Share on other sites

I tried changing my code to destroy the layers instead of the map, but it didn't seem to work. The generated "map" still looks the same:

 

Capture.png

 

Again, if I copy the array, convert it to 1D array, and paste it into my map.json's layer data, the map appears correctly. What else could be causing this?

Link to comment
Share on other sites

Sorry for the double post, but I was able to figure out part of my issue. I was not setting the tile size/width of my tileset image when adding it. After I added these parameters, the map started generating as it should look.

 

My only problem now is that the map is still loading above everything else. The player and HUD are hidden behind it. How do I get around this?

Link to comment
Share on other sites

Create the map first, then the player and HUD afterwards.

 

I understand this for initiating the game, but what if we are changing maps? The player and HUD already exist and recreating it all is kind of counter-intuitive. Is there any option other than completely remaking everything?

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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