Jump to content

Add text as child to group


KoMo
 Share

Recommended Posts

Hi Guys,

 

I was looking for an answer but could not find one so Im sorry if I missed and double post.

 

Im currently working on an small sidescroller game.

 

The enemies are created as group and come from the right into the scene.

 

what I would like to do is add a random name for the enemy displayed as text on top of the enemy,

and having the same physical propperties like it (eg gets killed/destroyd if the enemy dies, moves at same speed etc)

I suppose I can do this as child from the enemy but I have no idea how to add text this way especially for an "dynamic" text

 

I appreciate any help

 

cheers

Link to comment
Share on other sites

I think you can simply add your text as a child of your enemy sprite, e.g :

 

 

// your text : play with the 'x' and 'y' to position your text above your enemy sprite

 

var name = this.game.add.text(x, y, 'baddy', {font:'24px Arial', fill: '#FF0000', align: 'center'});

name.anchor.set(0.5)

 

// I suppose yourEnemy is a phaser sprite defined in your code

 

yourEnemy.addChild(name);

 

 

Now your text should position itself in relation to its parent. If you set x and y to 0, the center of your text should be positionned to the top-left of your enemy sprite

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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