Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to draw a bitmapfont on a stage in libgdx?

This is my current render method on my level in my Libgdx game. I am trying to draw a BitmapFont on my level's top right corner but all I'm getting 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 the score font into some kind of an actor and then do a scene.addActor(myScore) but I don't know how to do that. I followed Steigert's tutorial to create the main game class that instantiates the scene, font, in the AbstractLevel class that is then extended by this level.

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

like image 839
Peter Poliwoda Avatar asked Nov 09 '12 23:11

Peter Poliwoda


1 Answers

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

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 that that isn't the issue. You can just get it to return a float or an int and when you do the "Score:"+ bit it will turn it into a string itself

like image 175
LiamJPeters Avatar answered Sep 20 '22 14:09

LiamJPeters