Sign in to follow this  
Ninjadoodle

Align / Center Text

Recommended Posts

Hi @enpu

I'm having a little bit of trouble with aligning text.

I have a countdown time which basically starts at 20 and counts down to 0. Th issue I'm having is that it's always either left / right justified, when it get's updated.

Do I need to manually recenter the text every time it gets updated with a new number?

This is the code I'm using to set it up. (I've tried both 'align' and 'anchor' separately).

this.countdownTxt = new game.Text('20', {
    align: 'center'
});
this.countdownTxt.anchor.set(this.countdownTxt.width / 2, this.countdownTxt.height / 2);
this.countdownTxt.addTo(this.fg);
this.countdownTxt.position.set(640, 64);

Thanks in advance!

Share this post


Link to post
Share on other sites

align property is used only on multi-line text, so you can remove that.

To recenter you do have to set the x anchor after you change your text (because the width of the text changes), like this:

this.countdownTxt.setText('9');
this.countdownTxt.anchor.x = this.countdownTxt.width / 2;

 

Share this post


Link to post
Share on other sites
On 7/10/2018 at 7:45 PM, enpu said:

align property is used only on multi-line text, so you can remove that.

To recenter you do have to set the x anchor after you change your text (because the width of the text changes), like this:


this.countdownTxt.setText('9');
this.countdownTxt.anchor.x = this.countdownTxt.width / 2;

 

thanks you.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.