Jump to content

[1.1.6] How to clear a tilemap to draw a new level?


toto88x
 Share

Recommended Posts

Hi,

 

I'm building a platformer with Phaser 1.1.6. I managed to display the first level, and I have the player jumping around. Now I'm trying to load a new level when the player go to the finish line. 

 

Is there a best practice to load multiple levels from multiple json?

And how do I clean up the current tilemap before loading the new one? I tried map.destroy() and map.removeAllLayers() with no sucess.

 

Thanks!

Link to comment
Share on other sites

I actually do this in my game and it works really well. I have a single game state and use tile functions to clear and load a new map, as well as resetting the player x/y etc for the new level. What I do is have the layer destroyed, then create the variable layer again but with the new level data and JSON. I'll post an example of it. I'm on my phone at the minute.

Link to comment
Share on other sites

var tileFunc5 = {    mapChange: function()    {        level -= 0.5;        if(level === 1)        {            layer.destroy();            map = this.add.tilemap('level02');            map.addTilesetImage('Space02', 'tiles');            layer = map.createLayer('Space02');            layer.resizeWorld();            player.x = 61;            player.y = 391;            currentLevelX = 61;            currentLevelY = 391;            level = 2.5;        }        if(level === 2)        {            layer.destroy();            map = this.add.tilemap('level03');            map.addTilesetImage('Space03', 'tiles');            layer = map.createLayer('Space03');            layer.resizeWorld();            player.x = 130;            player.y = 710;            currentLevelX = 130;            currentLevelY = 710;            level = 3.5;        }        if(level === 3)        {            layer.destroy();            map = this.add.tilemap('level04');            map.addTilesetImage('Space04', 'tiles');            layer = map.createLayer('Space04');            layer.resizeWorld();            player.x = 150;            player.y = 450;            currentLevelX = 150;            currentLevelY = 450            level = 4.5;        }        if(level === 4)        {            layer.destroy();            map = this.add.tilemap('level05');            map.addTilesetImage('Space05', 'tiles');            layer = map.createLayer('Space05');            layer.resizeWorld();            player.x = 125;            player.y = 3603;            currentLevelX= 125;            currentLevelY = 3603;                        level = 5.5;        }        if(level === 5)        {            game.state.start('Cutscene1');            level = 6.5;        }        deathText.destroy();        player.bringToTop();        gamePaused.bringToTop();        map.setCollisionBetween(211, 217); // Main Floor        map.setCollisionBetween(246, 252); // stops glitching        map.setCollisionBetween(71,72);        map.setCollisionBetween(106,107); // blue lit box        map.setCollisionBetween(141,142);        map.setCollisionBetween(176, 177); // unlit box        map.setCollision(143); // small box        map.setCollisionBetween(178, 179); // 2 small boxes                 map.setCollisionBetween(146, 148); // black and yellow area        map.setCollisionBetween(334, 336); // black and yellow area        map.setCollisionBetween(369, 372); // black and yellow area        map.setCollisionBetween(181, 182); // black and yellow area        map.setCollision(301);        map.setCollision(299);     //   map.setTileIndexCallback([146,147], tileFunc1.emitBlock, this);        map.setTileIndexCallback(179, tileFunc2.gravSwitch, this);        map.setTileIndexCallback(143, tileFunc3.switchGrav, this);        map.setTileIndexCallback([168,2], tileFunc4.playerReset, this);        map.setTileIndexCallback(69, tileFunc5.mapChange, this);        deathText = this.add.text(10,10,'Deaths: 0',{        font: "22px Courier",         fill: "#19cb65",         stroke: "#119f4e",         strokeThickness: 2        });        deathText.fixedToCamera = true;        deathText.color = 0xff0091;        if (this.game.device.iOS || this.game.device.android)        {            jButton.bringToTop();        }               player.collideWorldBounds = true;    },}

Its a bit long winded because i have it setting a lot up for each time its fired. 

Have a scope through it and if there's something not making sense i'll be happy to tell you why or what i have done.

 

:)

Link to comment
Share on other sites

Thanks, layer.destroy() works! :-)

 

For your information, I think you could reduce your code by doing something like this before your if (level == x)

layer.destroy();map = this.add.tilemap('level'+level);map.addTilesetImage('Space'+level, 'tiles');layer = map.createLayer('Space'+level);layer.resizeWorld();
Link to comment
Share on other sites

yeah but the JSON holds the names and if anything is different from the JSON then it wont load. I'm not sure your example would work but give it a go and try.

I know its long but it works well and all the JSON data is called correctly. You can try it though and let me know if you got it working :)

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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