LibGDX BitmapFont不会停止颤抖

时间:2014-03-08 14:41:33

标签: java android libgdx shake bitmap-fonts

我有一个BitmapFont,当他以恒定速率在屏幕上移动时显示玩家的分数。因为玩家总是在移动,所以我必须重新计算每帧画出字体的位置。我使用这段代码。

    scoreFont.setScale(4f, 4f);
    scoreFont.draw(batch, "" + scoreToShow, playerGhost.pos.x + 100f, 600f);
    playerGhost.render(batch);
  • 问题?字体不会停止抖动。它只有几个像素的振动,但它有点明显。当我在平板电脑上运行时,它会更加引人注目。

  • 这是一个已知的错误吗?

  • 我怎样才能让它停止摇晃?

3 个答案:

答案 0 :(得分:8)

调用scorefont.setUseIntegerPositions(false);,这样它就不会将字体的位置舍入到最接近的整数。您可能还希望将字体的最小过滤设置为Linear或MipmapLinearNearest,并将max过滤设置为Linear。

默认行为的原因是默认配置适用于像素完美的文本,对于设置单位等于像素大小的视口。如果您的视口的尺寸与屏幕的像素尺寸完全相同,则此配置有助于防止文本看起来有些模糊。

答案 1 :(得分:3)

It could actually be the fact that you're scaling your font.

I had this problem and it's quite complex to understand (and also to fix).

Basically, when you scale fonts, BitmapFont changes the values inside the BitmapFontData by dividing/multiplying. If you do a lot of scaling, with a lot of different values (or an unlucky combination of values), it can introduce rounding errors which can cause flickering around the edges of the font.

The solution I implemented in the end was to write a Fontholder which stores all of the original BitmapFontData values. I then reset the font data to those original values at the beginning of every frame (i.e. start of render() method).

Here's the code...

$string = str_replace(['<br>','<br />','<p>','</p>'], '', $string);

I'm not wild about this, as it's slightly hacky, but it's a necessary evil, and will completely and properly solve the issue.

答案 2 :(得分:1)

处理这个的正确方法是使用两个不同的相机和两个不同的spriteBatches,一个用于游戏本身,另一个用于UI。 您在两个摄像头上调用update()方法,并在每个批处理中使用spriteBatch.setProjectionMatrix(camera.combined);在每个帧的同一时间渲染它们。