Kraken Posted May 7, 2017 Share Posted May 7, 2017 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 More sharing options...
samme Posted May 7, 2017 Share Posted May 7, 2017 In GameState#create, this is the current state, but in TowerDefense.UnitAI#damage, this is the unit/sprite. Kraken 1 Link to comment Share on other sites More sharing options...
Jem Kazama Posted May 7, 2017 Share Posted May 7, 2017 What samme said. You should be able to change if(this.health <=0){ this.monstersKilled++; } to if(this.health <=0){ TowerDefense.GameState.monstersKilled++; } Or whatever you named the state -- you just gotta access the correct variable. Kraken 1 Link to comment Share on other sites More sharing options...
Kraken Posted May 8, 2017 Author Share Posted May 8, 2017 Thanks, that did the trick! Link to comment Share on other sites More sharing options...
Jem Kazama Posted May 8, 2017 Share Posted May 8, 2017 Happy to help Kraken 1 Link to comment Share on other sites More sharing options...
Recommended Posts