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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.