Jump to content

Enemy kill counter not updating


Kraken
 Share

Recommended Posts

I'm trying to calculate the number of enemies killed but for some reason it is not updating.

I have a monstersKilled = 0 variable in the create section of the GameState.

this.monstersKilled = 0;

I made an enemy prefab where in the damage prototype I want this.monsterKilled to update as enemy's die but it is not updating.

var TowerDefense = TowerDefense || {};

TowerDefense.UnitAI = function (game, x, y, id, health) {

	Phaser.Sprite.call(this, game, x, y, id);
    
    this.customProps = {
		speed: 100
	};
    //this.game = game;
    
    this.game.physics.arcade.enable(this);
    this.scale.x = -1;
    this.body.velocity.x = -100;
    this.checkWorldBounds = true;
    this.body.collideWorldBounds = false;
    this.body.gravity.y = 1400;
    this.customProps.speed = 100;
    this.health = health;
   
};

TowerDefense.UnitAI.prototype = Object.create(Phaser.Sprite.prototype);
TowerDefense.UnitAI.prototype.constructor = TowerDefense.UnitAI;

TowerDefense.UnitAI.prototype.update = function() {
    if (this.customProps.speed) {
		this.body.velocity.x = Math.max(-this.customProps.speed, this.body.velocity.x - 6);
	}
    Phaser.Sprite.prototype.update.call(this);
};
TowerDefense.UnitAI.prototype.reset = function(x, y, id, health){
	
	Phaser.Sprite.prototype.reset.call(this, x, y, health);
    
    this.loadTexture(id);
    
};
TowerDefense.UnitAI.prototype.damage = function(amount) {
	Phaser.Sprite.prototype.damage.call(this, amount);
    
    if(this.health <=0){
      this.monstersKilled++;   
    }

};

here is my create enemy function to reset the enemies as needed

createEnemy: function(x, y, id, health){
     var monster = this.enemyUnits.getFirstExists(false);
      
     if(!monster){
         monster = new TowerDefense.UnitAI(this.game, x, y, id, health);
         this.enemyUnits.add(monster);
         monster.animations.play('run', 16, true);
         monster.body.setSize(58,83, 10, 11);
     }
         monster.reset(x, y, id, health);
         monster.animations.add('run');
         monster.animations.play('run', 16, true);
         monster.body.setSize(28,48, 0, 0);
  }

 

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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