LibGDX - 在tile / tilemaps中的纹理上的文本

时间:2013-12-22 23:38:38

标签: java android libgdx

有没有办法将文本放在Tilemaps中的纹理上?

我正在使用基于跳棋的游戏,我需要在单个磁贴中显示有多少个跳棋。我使用TiledMap加载电路板。我的代码如下:

GameScreen.java

public class GameScreen implements Screen {

private TiledMap board;

public GameScreen(final MyGame gam) {

   board = new TmxMapLoader().load("maps/tablero-64.tmx");
   renderer = new OrthogonalTiledMapRenderer(board, 1/64f);

   boardCamera = new OrthographicCamera();
   boardCamera.setToOrtho(false, WIDTH, HEIGHT);
   boardCamera.position.set(CENTERX, CENTERY, 0);

   // Added this to avoid continuous rendering
   Gdx.graphics.setContinuousRendering(false);
   Gdx.graphics.requestRendering();
}

@Override
public void render(float delta) {
   boardCamera.update();
   renderer.setView(boardCamera);
   renderer.render();
   renderer.getSpriteBatch().begin();
   for (Stack checker : whiteCheckers) checker.draw(renderer.getSpriteBatch(), 1);
   for (Stack checker : blackCheckers) checker.draw(renderer.getSpriteBatch(), 1);
   renderer.getSpriteBatch().end();
}
}

Stack.java

public class Stack extends Actor {

   static BitmapFont font = new BitmapFont(Gdx.files.internal("fonts/estragelo.fnt"), Gdx.files.internal("fonts/estragelo.png"), true);  // Line 31

   @Override
   public void draw(SpriteBatch batch, float parentAlpha) {
      batch.draw(this.texture,
            this.boardPosition.x,
            this.boardPosition.y,
            this.size,
            this.size);
      // This DOESN'T work
      font.draw(batch, "10", this.boardPosition.x, this.boardPosition.y); // Line 296
   }
}

提前致谢。

编辑:我添加错误日志:

12-24 17:49:18.178: E/AndroidRuntime(3365): FATAL EXCEPTION: GLThread
12-24 17:49:18.178: E/AndroidRuntime(3365): java.lang.ExceptionInInitializerError
12-24 17:49:18.178: E/AndroidRuntime(3365):     at my.package.GameScreen.<init>(GameScreen.java:81)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at my.package.MainMenuScreen.render(MainMenuScreen.java:36)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.Game.render(Game.java:46)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at my.package.MyGame.render(MyGame.java:23)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:487)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.guardedRun(GLSurfaceViewCupcake.java:713)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.backends.android.surfaceview.GLSurfaceViewCupcake$GLThread.run(GLSurfaceViewCupcake.java:646)
12-24 17:49:18.178: E/AndroidRuntime(3365): Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error loading font file: fonts/estragelo.fnt
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:971)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:135)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.graphics.g2d.BitmapFont.<init>(BitmapFont.java:127)
12-24 17:49:18.178: E/AndroidRuntime(3365):     at my.package.Stack.<clinit>(Stack.java:31)
12-24 17:49:18.178: E/AndroidRuntime(3365):     ... 7 more
12-24 17:49:18.178: E/AndroidRuntime(3365): Caused by: java.lang.ArrayIndexOutOfBoundsException
12-24 17:49:18.178: E/AndroidRuntime(3365):     at com.badlogic.gdx.graphics.g2d.BitmapFont$BitmapFontData.<init>(BitmapFont.java:836)
12-24 17:49:18.178: E/AndroidRuntime(3365):     ... 10 more

编辑2:问题是字体。 Estragelo字体生成两个PNG文件,但您只能在BitmapFont构造函数中指定一个。所以我改为Arial字体,它的工作原理。但是现在我遇到了与开始时相同的问题。这给了我相同的结果:

public void draw(SpriteBatch batch, float parentAlpha) {
    BitmapFont font = new BitmapFont();
    font.draw(batch, "10", this.boardPosition.x, this.boardPosition.y);
}

在某种程度上,它正在绘制数字,但位置不好,尺寸不好。这是结果(boardPosition是(4,0)和(3,7),其中放置了检查器):

This is the new result

1 个答案:

答案 0 :(得分:1)

新答案:

由于您的新BitmapFont字体是静态的,因此在初始化Gdx之前会加载它。这就是为什么它不会工作。为避免这种情况,您可以将静态字段font留空,并将其分配到GameScreen构造函数中:

public class Stack extends Actor {

   static BitmapFont font;

   @Override
   public void draw(SpriteBatch batch, float parentAlpha) {
   //your draw stuff
   }
}

然后在构造函数中分配字体:

public class GameScreen implements Screen {

public GameScreen(final MyGame gam) {
//your initializers and settings
Stack.font=new BitmapFont(Gdx.files.internal("fonts/estragelo.fnt"), Gdx.files.internal("fonts/estragelo.png"), true); 
}

旧答案:

您必须使用Hiero或[BMFont]等工具创建BitmapFonthttp://www.angelcode.com / products / bmfont /)创建一个字体并使用如下结果文件:

font=new BitmapFont(Gdx.files.local("fonts/font.fnt"),
Gdx.files.local("fonts/font.png"), true);

在我的情况下,字体文件位于我工作区的子文件夹“fonts”中。

在BMFont中,您可以选择字体的大小,样式等。

修改:你应该考虑noncontinuous rendering这个游戏,因为桌面游戏不需要每帧重绘图形。