Asg*_*eir 30 java graphics libgdx
我在使用Libgdx绘制如何绘制简单的2D文本时遇到了很多麻烦.这是我到目前为止编写的代码:
SpriteBatch spriteBatch;
BitmapFont font;
CharSequence str = "Hello World!";
spriteBatch = new SpriteBatch();
font = new BitmapFont();
spriteBatch.begin();
font.draw(spriteBatch, str, 10, 10);
spriteBatch.end();
Run Code Online (Sandbox Code Playgroud)
代码确实绘制了Hello World字符串,但是它会弄乱我所有的其他绘图.他们在那里,只是残忍地残缺,并且所有这一切都在移动.我试着Gdx.gl11.glPushMatrix()和Gdx.gl11.glPopMatrix()周围只是陈述的每一个子集.
我已经将残缺不全的图纸缩小到了font.draw()电话,如果它被取出,一切正常(但当然没有文字).
Dee*_*orn 24
我没有太多理由为文本绘图创建单独的批处理.使用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();
}
Run Code Online (Sandbox Code Playgroud)
注意,在这里你绘制游戏世界坐标,所以如果你的角色移动(例如在平台游戏中),那么文字也会移动.如果你想看到文本,它将被固定在屏幕上,比如Label/TextField或者它在不同的UI框架中的调用方式,比我建议使用Stage(和TextArea用于文本),请参阅示例如何使用在这里演出:http://www.toxsickproductions.com/libgdx/libgdx-basics-create-a-simple-menu/
小智 13
当我创建Bitmap字体时,它是这样的:
font = new BitmapFont(Gdx.files.internal("Calibri.fnt"),Gdx.files.internal("Calibri.png"),false);
Run Code Online (Sandbox Code Playgroud)
通过下载一些字体文件来尝试这一点(下载字体时,请检查是否包含.fnt文件和.png文件)
小智 6
要创建 .fnt 文件,请使用 LibGDX 网站提供的 hiero。
将字体大小设置为 150,它将创建一个.fnt文件和一个.png文件。将这两个文件复制到您的资产文件夹中。
声明字体:
BitmapFont font;
Run Code Online (Sandbox Code Playgroud)
加载字体:(在创建方法中)
font = new BitmapFont(Gdx.files.internal("data/rayanfont.fnt"), false);
//rayanfont is the font name
Run Code Online (Sandbox Code Playgroud)
渲染:
batch.begin();
font.setScale(.2f);
font.draw(batch, "hello", x,y);
batch.end();
Run Code Online (Sandbox Code Playgroud)
这将顺利进行。
尝试调用多个batch.begin()和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();
Run Code Online (Sandbox Code Playgroud)
或者只是使用不同的 SpriteBatch 实例