How to draw bitmapfont on stage in libgdx?

This is my current render method at my level in my Libgdx game. I'm trying to draw a BitmapFont in the upper right upper corner, but all I get is a bunch of white boxes.

@Override public void render( float delta ) { Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT); this.getBatch().begin(); //myScore.getCurrent() returns a String with the current Score font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500); stage.act(Gdx.graphics.getDeltaTime()); stage.draw(); this.getBatch().end(); } 

I would like to add a rating font to some actor and then make the scene .addActor (myScore), but I don't know how to do it. I followed the Steigert tutorial to create the main class of the game, which creates an instance of the scene, a font, in the AbstractLevel class, which then expands to this level.

So far I have not used any custom font, just an empty new BitmapFont (); to use the default Arial font. Later I would like to use my own more fancy font.

+7
source share
4 answers

Try moving the font.draw file after stage.draw. Adding it to an actor would be very simple, just create a new class and produce an actor. Like

 import com.badlogic.gdx.graphics.g2d.BitmapFont; import com.badlogic.gdx.graphics.g2d.SpriteBatch; import com.badlogic.gdx.scenes.scene2d.Actor; public class Text extends Actor { BitmapFont font; Score myScore; //I assumed you have some object //that you use to access score. //Remember to pass this in! public Text(Score myScore){ font = new BitmapFont(); font.setColor(0.5f,0.4f,0,1); //Brown is an underated Colour } @Override public void draw(SpriteBatch batch, float parentAlpha) { font.draw(batch, "Score: 0" + myScore.getCurrent(), 0, 0); //Also remember that an actor uses local coordinates for drawing within //itself! } @Override public Actor hit(float x, float y) { // TODO Auto-generated method stub return null; } } 

Hope this helps!

Edit 1: Also try System.out.println(myScore.getCurrentScore()); Just to make sure this is not a problem. You can simply return it to return a float or int, and when you execute the "Score:"+ bit, it will turn it into a string.

+12
source

Well, in this case, you may need to call this.getBatch().end . Like this:

 mSpriteBatch.begin(); mStage.draw(); mSpriteBatch.end(); //Here to draw a BitmapFont mSpriteBatch.begin(); mBitmapFont.draw(mSpriteBatch,"FPS",10,30); mSpriteBatch.end(); 

I don’t know why, but it works for me.

+3
source

I solved a similar problem with white boxes by closing the batch before drawing. This is because stage.draw () starts another batch and invalidates the previous batch that did not end with end ().

So, in the current example, I would move this.getBatch (). end () to the drawing step:

  ... font.draw(this.getBatch(), "Score: 0" + myScore.getCurrent(), 600, 500); this.getBatch().end(); stage.act(Gdx.graphics.getDeltaTime()); stage.draw(); } 
+1
source

If you are using Scene2D, try using the scene and actors. No need to write long codes, check that the MTX plugin is very easy to use. You can create an awesome user interface.

http://moribitotechx.blogspot.co.uk/

-2
source

All Articles