简体   繁体   English

Libgdx如何显示文本?

[英]Libgdx How Do I display Text?

Ok I have been searching so long for this, and I can never seem to find a good solution for my problem. 好的,我一直在寻找这么久,我似乎永远无法找到解决问题的好方法。 I know that the TextArea class in libgdx is used for user input. 我知道libgdx中的TextArea类用于用户输入。 People use it so that user can input multiple lines of a String. 人们使用它,以便用户可以输入多行String。 I need something like a TextArea that does the opposite. 我需要像TextArea这样的东西。 I want to display text to the user in a specific area for (Just like the text area), but I want it to be in some sort of window within my game (kinda like a table or a dialog box only without prompting the user for input). 我想在特定区域向用户显示文本(就像文本区域一样),但我希望它在我的游戏中的某种窗口中(有点像桌子或对话框而不提示用户输入)。 Can ANYONE help me with this? 谁能帮我这个? or is this something I just have to make from scratch? 或者这是我必须从头开始制作的东西?

Using gdxVersion = '1.4.1' (built with gradle in Android Studio) that code draws text successfully: 使用gdxVersion ='1.4.1'(在Android Studio中使用gradle构建)代码成功绘制文本:

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

Credit goes to @Deepscorn 归功于@Deepscorn

For more you can go through this tutorials: 有关更多信息,您可以阅读本教程:

  1. Libgdx tutorial for simply display the words Hello World on screen. Libgdx教程只是在屏幕上显示单词Hello World。
  2. How can I draw text using Libgdx/Java? 如何使用Libgdx / Java绘制文本?
  3. libGDX - Add scores & display it at top left corner of the screen? libGDX - 添加分数并将其显示在屏幕的左上角?

You can use the Label class and set a background drawable in the LabelStyle : 您可以使用Label类并在LabelStyle设置背景drawable:

Label.LabelStyle style = new LabelStyle();
style.background = ...; // Set the drawable you want to use

Label label = new Label("Text here", style);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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