2

I have a FlxText object which has properties like:

var CharHP:FlxText = new FlxText(1000, 500);
CharHP.Text = "HP: " + "9999";
CharHP.width = 300; CharHP.height = 200;
CharHP.alignment = "center";

I'm trying to align CharHP to center and I expect these coordinates:

1000+(300/2), 500+(200/2)

However, the text is at:

1000, 500

Gama11
  • 31,714
  • 9
  • 78
  • 100
Enes F.
  • 406
  • 6
  • 17

2 Answers2

2

Text alignment centers the visual display of the text itself rather than the text box itself. The text box itself will always be "at" the x/y you supply in the constructor or by setting x and y directly.

Example:

[Left aligned text        ]
[       Right aligned text]
[   Center aligned text   ]

In all three cases the coordinates and size of the text box itself are the same, but the visual position of the text itself is different.

larsiusprime
  • 935
  • 6
  • 14
  • You just wrote [ Center aligned text ]. How can I set width between[ and ] ? – Enes F. Dec 20 '15 at 20:28
  • 2
    @EnesF. You have to give the third argument to the FlxText constructor which is a FieldWidth, ex: FlxText(1000, 500, 300). – Gregory Dec 21 '15 at 00:17
0

This works for me. Not sure why I don't have to import FlxTextAlign to get this working but feel free to refer to the API docs for more info. https://api.haxeflixel.com/flixel/text/FlxTextAlign.html

import flixel.text.FlxText;

...

var text = new FlxText( 300, "Your text to center", 24); // the field width argument is important
text.alignment = FlxTextAlign.CENTER; // This can be LEFT, RIGHT, JUSTIFY
Richard Oliver Bray
  • 1,043
  • 13
  • 20