Jump to content

Fixing text on sprite


kray
 Share

Recommended Posts

How can I fix some text on top of a sprite so the text moves with it? 
Suppose I have different levels, with a question on top of a sprite, creating one sprite for a question would be highly unreasonable. So how can I use the same sprite and change the text on top of it, and make sure the text remains fixed on the sprite?

Link to comment
Share on other sites

You can create the underlying sprite first and then place the text at that sprite's coordinates. Then you have the choice of moving them both separately using tweens or grouping them and moving the group using tweens.

create function() {  // Add initial sprite to put text on.  var baseSprite = game.add.sprite(320, 320, 'spriteKey');  baseSprite.anchor.setTo(0.5, 0.5);    // Add text sprite second so it renders on top.  var spriteText = game.add.text(baseSprite.x, baseSprite.y, 'Sprite Text');  spriteText.anchor.setTo(0.5, 0.5);  // Create the group and place both sprites in it. Allows you to move the group instead.  var spriteGroup = game.add.group();  spriteGroup.addMultiple([baseSprite, spriteText]);  // You can now just tween the group instead and it will move both sprites.  var moveGroupTween = game.add.tween(spriteGroup).to({x: 100, y: 100}, 1000, Phaser.Easing.Linear.None, true);}

Finally you can simply set the text of the text sprite when you need to change it, that way you only end up using a single text sprite.

spriteText.setText('New sprite text');
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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