Jump to content

Level restart/change


Recommended Posts

I'm using a tilemap that gets modified during the game (basically a closed door tile becomes an open door tile).


I want to implement a restart level button but I don't really know which is the best way to do it.


I use the changeLevel function for both creating the first level and changing/restarting the current level.


It works fine except that:


1) Does not reload the tile from the json. It remembers the changes I did.


2) The game takes around 10MB more each time I execute this function and I much slower when scrolling.


Is there an example about this?

var LEVELS = ['level1', 'level2'];var CONTENT = [[{...}]];var changeLevel = function(puzzle, level, game) {    puzzle.layer = puzzle.map.createLayer(LEVELS[level]);  puzzle.layerName = puzzle.map.getLayer(LEVELS[level]);  puzzle.layer.resizeWorld();  puzzle.content = CONTENT[level];  if (puzzle.player != null)    puzzle.player.kill();  puzzle.player = game.add.sprite(puzzle.content[0].playerX, puzzle.content[0].playerY, 'all');  puzzle.player.animations.add('standing', [0, 1]);  ...  puzzle.player.animations.add('walking_item_right', [10, 11]);  puzzle.player.animations.play('standing', 1, true);  puzzle.game.camera.follow(puzzle.player);  puzzle.item = null;  puzzle.dir = 0; // 0=left, 1=right  for (var i = 1; i < CONTENT[level].length; ++i) {    var e = CONTENT[level][i];    if (e.type == 'key') {      if (e.sprite)        e.sprite.kill();      e.sprite = game.add.sprite(e.x, e.y, 'items');      e.sprite.animations.add('key', [2]);      e.sprite.animations.add('key_left', [0]);      e.sprite.animations.add('key_right', [1]);      e.sprite.animations.play(e.anim[0], 1, true);    }  }};

Thank you!

Link to comment
Share on other sites

I answer (partly) myself.


I've added a destroy before creating the layer and the does not slow anymore. Loading a new level increases slightly the memory usage but after a few seconds it goes down again (garbage collector?).


I still don't know the best way to "restart" the actual tile indexes. Recreate the whole map object?


This is the code.

  if (puzzle.layer)    puzzle.layer.destroy();

I got the idea looking at the code of Lessmilk's game #10. http://www.lessmilk.com/games/10/

But this game does not change the tile indexes.

Link to comment
Share on other sites

It seems obvious that in order to gain performances, you should just store everything done by the player during the game, and then reverse it on restart.


But basically, it's way easier to just restart everything.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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