简体   繁体   English

如何在 HaxeFlixel 中对齐文本?

[英]How to align text in HaxeFlixel?

I have a FlxText object which has properties like:我有一个FlxText对象,它具有如下属性:

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:我试图将CharHP对齐到中心,我希望这些坐标:

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

However, the text is at:但是,文本位于:

1000, 500

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.文本框本身将始终“位于”您在构造函数中提供的 x/y 或直接设置 x 和 y。

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.在这三种情况下,文本框本身的坐标和大小都相同,但文本本身的视觉位置不同。

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.不确定为什么我不必导入 FlxTextAlign 来使其正常工作,但请随时参考 API 文档以获取更多信息。 https://api.haxeflixel.com/flixel/text/FlxTextAlign.html 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM