xompl Posted May 13, 2014 Share Posted May 13, 2014 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 More sharing options...
xompl Posted May 13, 2014 Author Share Posted May 13, 2014 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 More sharing options...
valueerror Posted May 15, 2014 Share Posted May 15, 2014 why dont you use states and just restart the levelstate or start the next state? Link to comment Share on other sites More sharing options...
Heppell08 Posted May 15, 2014 Share Posted May 15, 2014 I've done this code myself with level changes and resets.My game code is built on it because of the arrangement but I posted a while ago about the actually level change code and got mine running. Have a look here: http://www.html5gamedevs.com/topic/4466-help-on-something-probably-quite-simple/ Link to comment Share on other sites More sharing options...
ZoomBox Posted May 15, 2014 Share Posted May 15, 2014 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 More sharing options...
Recommended Posts