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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.