Is there any way to put Text over a Texture in a Tilemaps?
I'm working in a checkers based game and I need to show how many checkers there are in a single tile. I use a TiledMap to load the board. My code is the following:
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
}
}
Thanks in advance.
EDIT: I add the error log:
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
EDIT 2: The problem was the font. Estragelo font generates two PNG files, but you can only specify one in BitmapFont constructor. So I changed to Arial font and it works. But now I have the same problem than at the beginning. This gives me the same result than doing:
public void draw(SpriteBatch batch, float parentAlpha) {
BitmapFont font = new BitmapFont();
font.draw(batch, "10", this.boardPosition.x, this.boardPosition.y);
}
In some way, it is painting the number but at bad position and bad size. This is the result (boardPosition is (4,0) and (3,7), where checkers are placed):
New Answer:
Since your new BitmapFont
font is static, itll be loaded before Gdx is initialized. Thats why it wont work. To avoid this you can leave the static field font
blank and assign it in the GameScreen
constructor:
public class Stack extends Actor {
static BitmapFont font;
@Override
public void draw(SpriteBatch batch, float parentAlpha) {
//your draw stuff
}
}
and then assign the font in the constructor:
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);
}
Old Answer:
You have to create a BitmapFont
with tools like Hiero or [BMFont](http://www.angelcode.com
/products/bmfont/) to create a font and use the resulting files like that:
font=new BitmapFont(Gdx.files.local("fonts/font.fnt"),
Gdx.files.local("fonts/font.png"), true);
In my case the font files are located in a subfolder "fonts" of my workspace.
In BMFont you can choose size, style etc of the font.
Edit: You should consider noncontinuous rendering for the game, because its not necessary for a board game to redraw graphics every frame.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With