Kraken 2 Report post Posted May 8, 2017 I'm loading my level data from JSON files and it keeps resetting the counter after you beat each level. Is there a way to keep the counter from resetting? Quote Share this post Link to post Share on other sites
jonteferm 5 Report post Posted May 8, 2017 Don't you have the counter as a variable global to the whole game? Where do you keep the counter? Post a code snippet 2 Kraken and Jem Kazama reacted to this Quote Share this post Link to post Share on other sites
Kraken 2 Report post Posted May 8, 2017 3 hours ago, jonteferm said: Don't you have the counter as a variable global to the whole game? Where do you keep the counter? Post a code snippet var TowerDefense = TowerDefense || {}; TowerDefense.GameState = { init: function(currentLevel) { this.monstersKilled = 0; this.numLevels = 2; this.currentLevel = currentLevel ? currentLevel : 'level1'; }, hitMonster: function(monster, weapon) { monster.body.velocity.x = 200; monster.body.velocity.y = -300; monster.damage(1); weapon.destroy(); if(!monster.alive){ this.killedEnemies++; if(this.killedEnemies == this.numEnemies) { this.game.state.start('GameState', true, false, this.levelData.nextLevel); } } }, TowerDefense.UnitAI.prototype.damage = function(amount) { Phaser.Sprite.prototype.damage.call(this, amount); if(this.health <=0){ TowerDefense.GameState.monstersKilled++; TowerDefense.GameState.monstersCountLabel.text = TowerDefense.GameState.monstersKilled; } So i have to JSON files that are levels and once a set amount of enemies are dead the level changes but it seems that when I start the state for the next level the entire game basically restarts. Quote Share this post Link to post Share on other sites
Jem Kazama 12 Report post Posted May 8, 2017 You're saying "start this state again" which looks to be hitting the init function again. Try using another function in your GameState object to handle going to the next level that you should call instead of restarting the state. Quote Share this post Link to post Share on other sites
Kraken 2 Report post Posted May 8, 2017 1 hour ago, MysticJ said: You're saying "start this state again" which looks to be hitting the init function again. Try using another function in your GameState object to handle going to the next level that you should call instead of restarting the state. Thanks for the reply I'm not really sure how the function could change the level can you show me an example? Quote Share this post Link to post Share on other sites
Kraken 2 Report post Posted May 9, 2017 13 hours ago, jonteferm said: Don't you have the counter as a variable global to the whole game? Where do you keep the counter? Post a code snippet Turns out this was the way to do it, thanks for the help! Quote Share this post Link to post Share on other sites
Jem Kazama 12 Report post Posted May 9, 2017 9 hours ago, Kraken said: Turns out this was the way to do it, thanks for the help! Glad that worked for you 1 Kraken reacted to this Quote Share this post Link to post Share on other sites