简体   繁体   中英

How do I get my LibGDX BitmapFont to draw?

I've searched all around for this problem, but can't find a solution.

This is my render loop:

 Gdx.gl.glClearColor(0, 0, 0, 1);
 Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

 batch.begin();

 BitmapFont font = new BitmapFont(Gdx.files.internal("data/Media/font/myfont1.fnt"), false);
 font.setColor(new Color(1, 1, 1, 1));
 font.draw(batch, "Hello", 100, 100);

 batch.end();

I've tried all possible colors, positions and different programs for generating fonts. But the result is always the same: A black screen! (when glClearColor is (1, 1, 1, 1), a white screen...) Can anyone tell me what is wrong?

Thanks in advance!

EDIT: I found the problem myself. It was a badly set up camera!

Seems that uhave not loaded the png file along with the fnt file

 font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);

And please never try to load anything in render method. Try to load the font in the constructor or else u will end up with a GC call and fps will eventually drop down

You can initallize a new BitmapFont without having .fnt in your project.

BitmapFont font = new BitmapFont();

then you render it:

batch.begin();
font.draw(batch, "Hello world", 200, 0);
batch.end();

don't forget that the Y axis starts from the bottom!

 font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), Gdx.files.internal("data/billy.png"), false);

如果字体图像很少,例如billy.fnt,billy_1.png,billy_2.png,则可以使用:

 font = new BitmapFont(Gdx.files.internal("data/billy.fnt"), false);

create a .fnt file using hiero which is provided by libgdx website

set the size of font 150 ,it will create a .fnt file and a png file

copy both of file in your assests folder

now declare the font

BitmapFont font;

nw in create method

font = new BitmapFont(Gdx.files.internal("data/rayanfont.fnt"), false);

//rayanfont is the font name you can give your font any name

in render

batch.begin();
font.setscale(.2f);
font.draw(batch, "hello", x,y);
batch.end();

this will work smoothly

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM