Sign in to follow this  
Kraken

Enemy kill counter not updating

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);
  }

 

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.