LibGDX And​​roid:在加载屏幕上看不到任何内容(渲染方法无法绘制)

时间:2016-07-12 09:31:53

标签: android graphics libgdx

当我在 Android 上启动我的应用程序时,加载屏幕上没有显示任何内容,它只是黑色。

当您启动应用程序时,某些资产正在加载时,应显示该屏幕。

Desktop 应用程序中的一切正常。 如何在 Android 上显示加载屏幕?

我在加载屏幕的show()中执行以下操作:

Assets.load();    //assets are being loaded there

cam = new OrthographicCamera();
viewport = new StretchViewport(width, height, cam);
viewport.apply();
cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2, 0);
cam.setToOrtho(false, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
batch.setProjectionMatrix(cam.combined);

sprite = new Sprite(new Texture(texturePath));
sprite.setPosition(100, 200);

我在加载屏幕的render()中执行以下操作:

Gdx.gl.glClearColor(.17f, .17f, .25f, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

cam.update();
batch.setProjectionMatrix(cam.combined);

batch.begin();
sprite.draw(batch);
batch.end();

if(Assets.manager.update())
    game.setScreen(nextScreen);

1 个答案:

答案 0 :(得分:0)

我找到了让它发挥作用的方法: 在加载屏幕之前显示另一个空白屏幕。

我真的不明白为什么会有效,但我希望这会对某人有所帮助。 (如果你知道它的哪一部分能够发挥作用,那么如果你能提供帮助就会很好:D)

我空屏幕的一部分:

public class EmptyScreen implements Screen {

OrthographicCamera cam;
Viewport viewport;
SpriteBatch batch;
MyGame game;

public EmptyScreen(SpriteBatch batch, MyGame game) {
    this.game = game;
    cam = new OrthographicCamera();
    viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
    viewport.apply();

    this.batch = batch;
}

@Override
public void show() {

}

@Override
public void render(float delta) {
    game.setScreen(new LoadingScreen(game, batch));
}

我的一部分(现在正在处理Android)加载屏幕

public class LoadingScreen implements Screen {

MyGame game;
Sprite sprite;
OrthographicCamera cam;
Viewport viewport;
Stage stage;
SpriteBatch batch;

public LoadingScreen(MyGame game, SpriteBatch batch) {
    this.game = game;
    cam = new OrthographicCamera();
    viewport = new StretchViewport(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), cam);
    viewport.apply();

    cam.position.set(cam.viewportWidth / 2, cam.viewportHeight / 2, 0);

    this.batch = batch;

    batch.setProjectionMatrix(cam.combined);

    sprite = new Sprite(new Texture(texturePath));
    //you may need to set the position of the sprite here
}

@Override
public void show() {
    stage = new Stage(viewport, batch);
    Assets.load();
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(.1f, .2f, .25f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

    batch.setProjectionMatrix(cam.combined);  

    batch.begin();
    sprite.draw(batch);  
    batch.end();

    if(Assets.manager.update())
        game.setScreen(nextScreen);
    }