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;
因此tBounds1和tBounds2指向同一对象。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指向不同的对象。是的,我曾尝试分配给基本类型,但我浪费了一个小时试图首先找到问题。哦,好吧