LIBGDX White textures in the first run on android

自作多情 提交于 2019-12-13 05:03:48

问题


I created a game using LIBGDX and it works fine in desktop. When I install it and open it using the install wizard(?) It works fine until I go home or lock it and then resume it. Then it shows only white boxes instead of texture and when I go home or lock it the sounds are still running. After I exit completely the sounds stop running. But when I run it again the error doesn't happen again no matter how many times I open the game.

It only happens in that case, if i fist close the install wizard and open it normally the problem doesn't happen either.

This is my code:

public class MyGame extends Game {

public static final AssetManager MANAGER = new AssetManager();

public SpriteBatch SB;

public final AbstractScreen GAMEOVER, LOADING, MAIN, GAMEPLAY;

public MyGame() {
    GAMEOVER = new GameOverScreen(this);
    GAMEPLAY = new GameplayScreen(this); 
    LOADING = new LoadingScreen(this);
    MAIN = new MainScreen(this); 
}

@Override 
public void create() { 
    Texture.setEnforcePotImages(false); 

    SB = new SpriteBatch();

    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);


    MANAGER.load("xxx.wav", Sound.class);
    MANAGER.load("xxx.wav", Sound.class);

    setScreen(LOADING);
}

@Override
public void dispose() {
    super.dispose();
    MANAGER.dispose();
    SB.dispose();
}
}

Also i use different screens.

I hope someone could help me with this.

EDIT. Following what Basimm answered i added this to my code:

@Override
public void pause() {
    MANAGER.clear();
}

@Override
public void resume() {
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);
    MANAGER.load("xxx.png", Texture.class);


    MANAGER.load("xxx.wav", Sound.class);
    MANAGER.load("xxx.wav", Sound.class);

    this.setScreen(LOADING);
}

I had to added the setScreen(LOADING); beacause when i locked the device the textures got white again.


回答1:


This is a common error because when the android device goes into a pause state the textures are automatically unloaded so if you look in you application listener there will be a resume() function that will be called when the application is loaded again, you will have to make sure that you reload all of your assets after it has been paused then resumed again.

So call your managers load functions in your resume method again because it will need to be loaded again, and call the manager's clear() function in the pause method so that it empties all loaded assets.



来源:https://stackoverflow.com/questions/23238681/libgdx-white-textures-in-the-first-run-on-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!