LibGDX字体没有出现在屏幕上

时间:2013-11-30 09:54:17

标签: java android fonts libgdx bitmap-fonts

我试图让我的文字出现,但我无法在屏幕上看到它。我已经玩了几次数字,发现LibGDX把字体画到异常大的尺寸,但我不知道我是否还需要缩小字体尺寸,或者我是否将它们画在屏幕外。我复制了整个代码并粘贴在下面。

我用Hiero在白色背景上制作了一个256x256位图的ariel black。

package com.me.manners;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.Texture.TextureFilter;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.me.input.Input;
public class Choice implements Screen
{
private PerspectiveCamera camera;
private SpriteBatch batch;
private Texture arrowT;
private Sprite arrowS;
private BitmapFont font;
private String str1 = "Hello World!";
private Input input = new Input();
private Manners game;

public Choice(Manners game)
{
    this.game = game;
    camera = new PerspectiveCamera();
    batch = new SpriteBatch();
    font = new BitmapFont(Gdx.files.internal("data/choice/ariel.fnt"), Gdx.files.internal("data/choice/ariel.png"),false);
    Gdx.input.setInputProcessor(input);

    arrowT = new Texture(Gdx.files.internal("data/choice/arrow.png"));
    arrowT.setFilter(TextureFilter.Linear, TextureFilter.Linear);
    arrowS = new Sprite(new TextureRegion(arrowT, 0, 0, 64, 64));
    arrowS.setSize(0.125f, 0.25f);
    arrowS.setPosition(-0.75f, 0);
}

@Override
public void dispose() {
    batch.dispose();
    arrowT.dispose();
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(camera.combined);
    batch.begin();
    arrowS.draw(batch);
    font.setColor(1.0f,1.0f,0.0f,1.0f);
    font.setScale(0.5f,0.5f);
    font.draw(batch, str1, Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.5f);
    batch.end();
}
}

2 个答案:

答案 0 :(得分:1)

我认为您的游戏是2D游戏,因此使用camera = new OrthographicCamera(viewportWidth, viewportHeight)可以解决您的问题。

答案 1 :(得分:1)

phucvin做了很好的观察。看来你的游戏是2d,所以你应该使用OrtographicCamera。我建议你使用大数字,因为BitmapFont将使用正确的像素大小进行渲染(这就是你看到非常大的字母的原因)。

camera = new OrthographicCamera(480, 320); //for example.

当然,您需要将Sprite大小更改为更大的

arrowS.setSize(100, 20); //for example

或者使用具有低视口值的第二个相机用于精灵。