Jump to content

How to keep score from resetting after every level


Kraken
 Share

Recommended Posts

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

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

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

 Share

  • Recently Browsing   0 members

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