Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
libgdx-scene2d.ui标签高度_Libgdx_Scene2d - Fatal编程技术网

libgdx-scene2d.ui标签高度

libgdx-scene2d.ui标签高度,libgdx,scene2d,Libgdx,Scene2d,为什么会这样 TextBounds tBounds1 = game.font.getWrappedBounds("blah\nblah\nblah", 300); System.out.println(""+tBounds1.height); // outputs 79.0001 TextBounds tBounds2 = game.font.getWrappedBounds("blah", 300); System.out.println(""+tBounds1.height); // out

为什么会这样

TextBounds tBounds1 = game.font.getWrappedBounds("blah\nblah\nblah", 300);
System.out.println(""+tBounds1.height); // outputs 79.0001
TextBounds tBounds2 = game.font.getWrappedBounds("blah", 300);
System.out.println(""+tBounds1.height); // outputs 20.00002

因此,只需对另一个变量调用
getWrappedBounds()
,变量
tBounds1
就改变了。这是怎么回事?

似乎tBounds1和tBounds2指向同一个对象

如果您尝试检查它们,则下面的Areame就是true

boolean areSame = tBounds1 == tBounds2;
因此tBounds1tBounds2指向同一对象。getWrappedBounds方法由以下方法调用:

public TextBounds getWrappedBounds (CharSequence str, float wrapWidth) {
    return getWrappedBounds(str, wrapWidth, cache.getBounds());
}
不是cache.getBounds。创建位图字体时,会在开始时创建缓存

private final BitmapFontCache cache;
因此,缓存是当前字体的一个属性,因此当某些内容在当前字体中更改时,它也会被传播

getWrappedBounds方法还调用另一个方法:

public TextBounds getWrappedBounds (CharSequence str, float wrapWidth, TextBounds textBounds) {
    // Just removed the lines here
    textBounds.width = maxWidth;
    textBounds.height = data.capHeight + (numLines - 1) * data.lineHeight;
    return **textBounds**;
}
最后,此方法正在更改BitmapFontCache缓存对象

因此,如果要计算两个不同字符串的高度,可以将其指定给基本类型:

float height1 = font.getWrappedBounds("blah\nblah\nblah", 300);
float height2 = font.getWrappedBounds("blah", 300);
或者,如果需要完整的BitmapFont.TextBounds对象,请执行以下操作:

BitmapFont.TextBounds tBounds1 = new BitmapFont.TextBounds(font.getWrappedBounds("blah\nblah\nblah", 300));
BitmapFont.TextBounds tBounds2 = new BitmapFont.TextBounds(font.getWrappedBounds("blah", 300));

通过这种方式,您可以确保tBounds1和tBounds2指向不同的对象。

是的,我曾尝试分配给基本类型,但我浪费了一个小时试图首先找到问题。哦,好吧