【问题标题】:LibGDX Android: nothing to see on loading screen (render method does not draw)LibGDX And​​roid:在加载屏幕上看不到任何东西(渲染方法不绘制)
【发布时间】:2016-07-12 09:31:52
【问题描述】:

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

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

桌面应用程序一切正常。 如何在 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);

【问题讨论】:

  • 而且日志中没有错误,对吧?
  • 不,我没有发现任何错误。而且我知道正在调用屏幕的render() 方法,但不知何故没有绘图。

标签: android graphics libgdx


【解决方案1】:

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

我真的不明白为什么会这样,但我希望这会对某人有所帮助。 (如果您知道其中的哪一部分使它起作用,那么如果您能提供帮助,那就太好了: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));
}

我的一部分(现在也在安卓上工作)加载屏幕

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);
    }

【讨论】:

    猜你喜欢
    • 2014-05-16
    • 2021-05-08
    • 2013-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-21
    • 2015-02-11
    相关资源
    最近更新 更多