Is it possible to have game.add.text("...") follow a sprite?

Hello guys. I’m kinda new to Phaser.

I’m finishing up my first project, and am trying to heal some of the biggest wounds. :slight_smile:

I’ve spent quite a few attempts on this, but moved on every time with the intent of comming back to it.

So, I’m thinking I need my game.add.text to use the position.x and position.y of one of my Sprites.

If I put it in create() the text is placed at the first x,y position and not updated again.
If i put it in update() the text is ruined when moving the Sprite as the text is turned unreadable quite fast as it’s rendered 60 times a sec on top of itself…

Hope someone can see a logical solution to this amateur problem.

maybe something like this:
inside create()

this.followText = this.add.text(0, 0, "My Name");

and inside update()

this.followText.setPosition(sprite.x, sprite.y);

You just need to creating the text object once and play with the position afterward

1 Like

Uncaught TypeError: this.followText.setPosition is not a function

Hmmm. Any ideas?

can you show some of relevant code?
I though this.followText is not created propely in yours

Are you sure this setPosition exists in the API? Never hard of it before. I’m also quite certain that if it existed the syntax would be sprite.position.x and not sprite.x?

Example:

for (const data of hintData) {
	const hintObject = this.add.text(0, 0, data.hintMessage);
	hintObject.setAlign('center')
		.setOrigin(0.5)
		.setPosition(data.x, data.y)
		.setFontSize(20);
	this._hintTexts.push(hintObject);
}

Yes, thats what I always working for.
And also pls read the doc: https://photonstorm.github.io/phaser3-docs/Phaser.GameObjects.Text.html#setPosition__anchor
Or maybe, just maybe, you’re in the old version of Phaser 3

setPosition has existed since the start of Phaser 3. Game Objects don’t keep their positions in vectors, so sprite.position wouldn’t exist.

game.add is a Phaser 2 API. Are you sure you’re on Phaser 3?

1 Like

No I’m on Phaser 2 CE, I thought the syntax was almost the same.
I guess it’s not at all. I’m sorry.