我如何使用Libgdx / Java绘制文本? [英] How can I draw text using Libgdx/Java?

查看:121
本文介绍了我如何使用Libgdx / Java绘制文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在搜索如何使用Libgdx绘制简单的2D文本方面遇到了很多麻烦。这里是我已经放在一起的代码:

  SpriteBatch spriteBatch; 
BitmapFont字体;
CharSequence str =Hello World!;
spriteBatch =新的SpriteBatch();
font = new BitmapFont();

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

代码确实绘制了Hello World字符串,但是,它弄乱了我所有其他的绘图。他们在那里,只残酷地肢解,移动等等。我已经在几乎每个子集上尝试了 Gdx.gl11.glPushMatrix() Gdx.gl11.glPopMatrix()的陈述。

我将缩略图缩小到 font.draw()调用,一切正常(但当然没有文字)。

解决方案

我没有看到创建单独批次的很多理由为文本绘图。使用gdxVersion ='1.4.1'(用Android Studio中的gradle构建)代码成功绘制文本:

  BitmapFont font = new BitmapFont(); //或使用alex答案来使用自定义字体

public void render(float dt)
{
batch.setProjectionMatrix(camera.combined); //或者你的矩阵绘制GAME WORLD,而不是UI

batch.begin();

//绘制背景,对象等
for(View view:views)
{
view.draw(batch,dt);
}

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

batch.end();

请注意,在这里您绘制了游戏世界坐标,所以如果您的角色移动(例如在平台游戏中),文本也会移动。如果你想看到文本,它会被固定在屏幕上,比如Label / TextField,或者它在不同的UI框架中被调用,比我推荐使用Stage(和TextArea作为文本),例如参见如何使用登台: http://www.toxsickproductions.com/libgdx / libgdx-basics-create-a-simple-menu /


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).

解决方案

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/

这篇关于我如何使用Libgdx / Java绘制文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆