libGDX FitViewport不适用于Android

时间:2015-01-27 12:31:11

标签: java android libgdx

我有这种方法可以在Windows桌面上按预期工作和扩展:

public class MyGdxGame extends ApplicationAdapter {
    SpriteBatch m_batch;
    FitViewport m_viewport;
    OrthographicCamera m_camera;

    private Texture img;
    private BitmapFont m_font;


    @Override
    public void create() {
        Gdx.app.setLogLevel(Application.LOG_NONE);

        m_batch = new SpriteBatch();

        m_camera = new OrthographicCamera();
        m_camera.position.set(640 / 2f, 400 / 2f, 0);
        m_camera.update();
        m_viewport = new FitViewport(640, 400, m_camera);

        img = new Texture("badlogic.jpg");

        m_font = new BitmapFont();
    }

    @Override
    public void render() {
        Gdx.gl.glClearColor(1, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        m_batch.begin();
        m_font.draw(m_batch, "X000000000000000000000000000000000000000000000000000000000000000000000000000000X", 0, m_camera.viewportHeight);
        m_batch.draw(img, 0, 0);
        m_batch.end();
    }

    @Override
    public void resize(int width, int height) {
        m_viewport.update(width, height);
    }
}

初始窗口大小: 01

更大的窗口大小 02

但如果我部署到Android(在本例中为Nexus 7 2013),它似乎无法正确扩展: 03

为什么不起作用?怎么解决这个问题?

1 个答案:

答案 0 :(得分:4)

您忘记在精灵批次中使用相机。呼叫

spriteBatch.setProjectionMatrix(viewport.getCamera().combined);
spriteBatch.begin();之前