Jump to content

How to attach text on enemies


lifesuxtr
 Share

Recommended Posts

  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

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

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

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

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

 Share

  • Recently Browsing   0 members

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