lifesuxtr Posted November 18, 2017 Share Posted November 18, 2017 this.zombies = this.game.add.group(); this.zombies.enableBody=true; for (var i = 0; i < 5; i++) { var zombie = this.zombies.create(this.game.world.randomX, this.game.world.randomY, 'zombie'); zombie.health=5; this.zombieHealthText=this.game.add.text(zombie.x, zombie.y, "5", { fontSize: '16px', fill: '#000' }); } I am trying to add text on moving enemies.This code creates text on their initial point.What should i write inside update function to make them stick to zombies while they are moving ? Link to comment Share on other sites More sharing options...
Odk Posted November 19, 2017 Share Posted November 19, 2017 As you already have zombies as group then I would add something like this (writing from top of my head) in your update function: this.zombies.forEachAlive(function(zombie) { zombie.zombieHealthText.x = zombie.x; zombie.zombieHealthText.y = zombie.y; }, this); Then it will update text object position in each frame. It's fairly simple operation so shouldn't be a performance issue on any device. Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 19, 2017 Author Share Posted November 19, 2017 17 minutes ago, Odk said: As you already have zombies as group then I would add something like this (writing from top of my head) in your update function: this.zombies.forEachAlive(function(zombie) { zombie.zombieHealthText.x = zombie.x; zombie.zombieHealthText.y = zombie.y; }, this); Then it will update text object position in each frame. It's fairly simple operation so shouldn't be a performance issue on any device. I tried something similar to this i cant reach coordinates Cannot set property x undefined error Link to comment Share on other sites More sharing options...
samid737 Posted November 19, 2017 Share Posted November 19, 2017 You can also add the text as A child: line 78 . in your case it would be zombie.addChild(this.zombieHealthText). Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 19, 2017 Author Share Posted November 19, 2017 11 minutes ago, samid737 said: You can also add the text as A child: line 78 . in your case it would be zombie.addChild(this.zombieHealthText). I tried that too this is the result: Link to comment Share on other sites More sharing options...
Odk Posted November 19, 2017 Share Posted November 19, 2017 1 hour ago, lifesuxtr said: I tried something similar to this i cant reach coordinates Cannot set property x undefined error Because you do: this.zombieHealthText=this.game.add.text .... So you are assigning new text object to same game object in each iteration. In result you have reference to just one text object. It should be zombie.zombieHealthText=this.game.add.text .... Then you will create text object for each zombie separately. Link to comment Share on other sites More sharing options...
lifesuxtr Posted November 19, 2017 Author Share Posted November 19, 2017 7 hours ago, Odk said: Because you do: this.zombieHealthText=this.game.add.text .... So you are assigning new text object to same game object in each iteration. In result you have reference to just one text object. It should be zombie.zombieHealthText=this.game.add.text .... Then you will create text object for each zombie separately. Thanks... it worked Link to comment Share on other sites More sharing options...
Recommended Posts