Sign in to follow this  
Kraken

How to keep score from resetting after every level

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.

Share this post


Link to post
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?

Share this post


Link to post
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!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.