Kraken Posted May 8, 2017 Share 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? Link to comment Share on other sites More sharing options...
jonteferm Posted May 8, 2017 Share 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 Kraken and Jem Kazama 2 Link to comment Share on other sites More sharing options...
Kraken Posted May 8, 2017 Author Share 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. Link to comment Share on other sites More sharing options...
Jem Kazama Posted May 8, 2017 Share 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. Link to comment Share on other sites More sharing options...
Kraken Posted May 8, 2017 Author Share 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? Link to comment Share on other sites More sharing options...
Kraken Posted May 9, 2017 Author Share 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! Link to comment Share on other sites More sharing options...
Jem Kazama Posted May 9, 2017 Share 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 Kraken 1 Link to comment Share on other sites More sharing options...
Recommended Posts