How can I draw text using Libgdx/Java?

和自甴很熟 提交于 2019-12-08 14:28:00

问题


I've been having a lot of trouble Googling how to draw simple 2D text with Libgdx. Here is the code that I've put together so far:

SpriteBatch spriteBatch;
BitmapFont font;
CharSequence str = "Hello World!";
spriteBatch = new SpriteBatch();
font = new BitmapFont();

spriteBatch.begin();
font.draw(spriteBatch, str, 10, 10);
spriteBatch.end();

The code does draw the Hello World string, however, it messes up all my other drawings. They are there, only brutally mutilated, and move and all that. I've tried Gdx.gl11.glPushMatrix() and Gdx.gl11.glPopMatrix() around just about every subset of statements.

I've narrowed the mutilated drawings down to the font.draw() call, if that's taken out, everything works fine (but of course there is no text then).


回答1:


I don't see much reason of creating separate batch for text drawing. Using gdxVersion = '1.4.1' (built with gradle in Android Studio) that code draws text successfully:

BitmapFont font = new BitmapFont(); //or use alex answer to use custom font

public void render( float dt )
  {
    batch.setProjectionMatrix(camera.combined); //or your matrix to draw GAME WORLD, not UI

    batch.begin();

    //draw background, objects, etc.
    for( View view: views )
    {
      view.draw(batch, dt);
    }

    font.draw(batch, "Hello World!", 10, 10);

    batch.end();
  }

Note, that here you draw in game world coordinates, so if your character moves (in platformer, for example), than text will move too. If you want to see text, that it will be fixed on screen, something like Label/TextField or how it is called in different UI frameworks, than I recommend to use Stage (and TextArea for text), see for example on how to use Stage here: http://www.toxsickproductions.com/libgdx/libgdx-basics-create-a-simple-menu/




回答2:


When I created the Bitmap font it was like this:

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

try this by downloading some font file(while downloading a font please check if .fnt file and .png file is included for the same)




回答3:


Try to call multiple batch.begin() and batch.end():

CharSequence str = "Hello World!";
spriteBatch = new SpriteBatch();
font = new BitmapFont();

spriteBatch.begin();
font.draw(spriteBatch, str, 10, 10);
spriteBatch.end();
spriteBatch.begin();
//draw your other sprites here
spriteBatch.draw(...);
spriteBatch.end();

or just use a different instance of SpriteBatch




回答4:


To create a .fnt file, use hiero which is provided by LibGDX's website.

Set the size of font 150, it will create a .fnt file and a .png file. Copy both files in your assets folder.

To declare the font:

BitmapFont font;

To load the font: (In create method)

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

To render:

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

This will work smoothly.



来源:https://stackoverflow.com/questions/12466385/how-can-i-draw-text-using-libgdx-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!