LibGDX getBounds没有返回正确的宽度 [英] LibGDX getBounds not returning correct width

查看:93
本文介绍了LibGDX getBounds没有返回正确的宽度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这里关注答案:如何在Libgdx中获得字符串宽度?

我遇到的问题是getBounds似乎没有返回正确的宽度.我下面的代码应在字符串的末尾放置点,但是您会看到点在整个文本中流血.如何获得正确的文字宽度?

The problem I am having is that the getBounds doesn't seem to return to correct width. My code below should place dots after the end of the string, but you can see dots bleeding through some of the text. How do I get correct width of the text?

for (HighScore s : this.scoresData){
    font.draw(batch, s.name, x, y);
    TextBounds nameBounds = font.getBounds(s.name);

    font.draw(batch, s.value, (float) (KineticAssaultMain.WIDTH / 1.5f), y);

    for (float i = (nameBounds.width + x); i < ((float)Screen.WIDTH / 1.5f);){
        TextBounds dot = font.draw(batch, ".", i, y);
        i += dot.width;
    }

    // go to the next line
    y -= 32;
}

推荐答案

在调用另一个BitmapFont方法后,不能保证从getBounds返回的TextBounds有效. TextBounds实例可在内部重用以避免分配.这是一个不幸的陷阱,但这是libgdx避免使用GC的结果.

The TextBounds returned from getBounds isn't guaranteed to be valid after calling another BitmapFont method. The TextBounds instance is reused internally to avoid allocation. It's an unfortunate gotcha but is a result of libgdx avoiding GC.

这篇关于LibGDX getBounds没有返回正确的宽度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆