libgdx/Android:应用程序销毁/暂停后,图形消失 [英] libgdx/Android: graphics disappearing after the app is destroyed/paused

查看:91
本文介绍了libgdx/Android:应用程序销毁/暂停后,图形消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些测试,我意识到在Nexus 5上,当我按下后退键(或房屋)时-即当上下文发生变化时-然后我返回对我的游戏来说,openGL上下文丢失了.

I was doing some tests and I realized that on my Nexus 5, when I hit the back key (or the home) -that is, when there's a change of context- and I go back to my game, the openGL context is lost.

不再有纹理(显示为黑色)或UI的外观(按钮为白色).

There are no textures anymore (they show as black) or skins for the UI (the buttons are white).

我认为它是由libgdx自动管理的,对吗?那为什么会这样呢?

I thought it was automanaged by libgdx automatically, right? So why is this happening?

我创建纹理的方式是通过TextureAtlas,例如

The way I'm creating the textures is via TextureAtlas, like

TextureAtlas atlas;
TextureRegion bg;
atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
bg = atlas.findRegion("bg");

然后与batch.draw(bg, x, y, w, h);

我还尝试创建直接加载纹理而不是TextureAtlasTextureRegion(以防万一,但应该相同),并且得到相同的结果...

I also tried creating the TextureRegion loading directly a Texture instead of a TextureAtlas (just in case but it should be the same) and I get the same result...

有人吗?

更具体的代码:

屏幕课程基础知识:

public class MainMenuScreen extends ScreenManager.Screen {

        private Game game;
    private InputMultiplexer inputMultiplexer = new InputMultiplexer();

    private MainMenuUi screenUi;
    private MainMenuView screenView;

    private TextureAtlas atlas;

    public MainMenuScreen(ConbiniGame game) {
        this.game = game;

        atlas = new TextureAtlas(Gdx.files.internal("mainMenu.atlas"));
        screenUi = new MainMenuUi(game);
        screenView = new MainMenuView(atlas);

        inputMultiplexer.addProcessor(screenUi.getInputProcessor());
        inputMultiplexer.addProcessor(screenView.getInputProcessor());

        Gdx.input.setInputProcessor(inputMultiplexer);
    }

    // ...
}

正在使用TextureAtlas的MainMenuView类...

MainMenuView class where the TextureAtlas is being used...

public class MainMenuView {

    private Stage stage;
    private OrthographicCamera camera;
    private Viewport viewport;

    private TextureAtlas atlas;
    TextureRegion bg;

    public MainMenuView(TextureAtlas atlas) {
        atlas = atlas;
        bg = atlas.findRegion("bg");

        camera = new OrthographicCamera();
        camera.setToOrtho(false);
        viewport = new FitViewport(1080, 1920, camera);
        stage = new Stage(viewport);
    }

    public void update(float delta) {
        stage.act(delta);
    }

    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.getBatch().begin();
        stage.getBatch().draw(bg, 0, 0, stage.getCamera().viewportWidth, stage.getCamera().viewportHeight);
        stage.getBatch().end();

        stage.draw();
    }

    public InputProcessor getInputProcessor() {
        return stage;
    }
}

代码仅用于显示纹理的使用,而其他部分已删除

The code is just to show the use of the texture, with other parts removed

推荐答案

您没有提供足够的信息,但是您的问题很可能是由于在代码中使用static引起的.不要那样做.

You didn't provide enough information, but your problem is likely caused by using static in your code. Don't do that.

当您按下后退"按钮时,您的应用程序将关闭.当您按下主屏幕按钮时,您的应用程序将暂停.请注意,这是两件事.因此,使用主页"按钮时,您可能并不总是遇到此问题.这是因为在您的应用暂停时,该android可能会决定关闭它(以释放内存),但不能保证会关闭它.

When you press the back button your app is closed. When you press the home button then your app is paused. Note that this are two different things. Therefor you might experience the problem not always when using the home button. This because while your app is paused, that android might decide to close it (to free memory), but it is not guaranteed to do so.

无论哪种方式,这都与丢失的opengl上下文无关.它刚刚关闭.如果上下文确实丢失,那么libgdx(和更高版本的android)将为您恢复上下文.

Either way, this is not related to the opengl context being lost. It is just closed. If the context would be really lost then libgdx (and later versions of android) will recover it for you.

当您关闭应用程序然后立即再次启动它时,Android可能会为该应用程序实例重用相同的VM.这也意味着任何static变量都将具有它们在上一次运行应用程序时所具有的值.如果这些变量中的任何一个包含(可能是间接的)任何资源,那么这些资源将不再有效.

When you close your app and then immediately start it again then Android might reuse the same VM for that instance of your app. This also means that any static variables will have the value they had from the previous run of your app. If any of those variables include (may be indirectly) any resources, then those resources won't be valid anymore.

tl; dr从不在Android应用程序中使用static.

tl;dr never use static in android applications.

最常见的错误(没有看到您的代码,这只是猜测)是使用单例来访问资产.例如. MyGame.getInstance().assets.get("atlas",...);请勿这样做,(由于上述原因)将失败.而是将对您的MyGame实例的引用传递给任何需要它的类(例如,您的Screen:new MenuScreen(this);).

The most common mistake (without seeing your code this is just guessing) is to access assets by using a singleton. E.g. MyGame.getInstance().assets.get("atlas",...); don't do that, it will (because of above reason) fail. Instead pass a reference to your MyGame instance to whichever classes needs it (e.g. your Screen: new MenuScreen(this);).

这篇关于libgdx/Android:应用程序销毁/暂停后,图形消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆