如何在libgdx的舞台上绘制bitmapfont?

Pet*_*oda 8 android stage game-engine bitmap-fonts libgdx

这是我目前在我的Libgdx游戏中的渲染方法.我试图在我的关卡右上角绘制一个BitmapFont,但我得到的只是一堆白盒子.

 @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();
        }
Run Code Online (Sandbox Code Playgroud)

我想将得分字体添加到某种演员中,然后做一个scene.addActor(myScore),但我不知道该怎么做.我按照Steigert的教程创建了主要的游戏类,它在AbstractLevel类中实例化了场景,字体,然后由这个级别进行扩展.

到目前为止,我没有使用任何自定义字体,只是空的新BitmapFont(); 用于默认Arial字体.后来我想用我自己更花哨的字体.

Lia*_*ers 12

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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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