如果在启动画面打开时中断游戏崩溃 - LIBGDX

2022-01-12 00:00:00 java libgdx android-activity scene2d

I have a splash screen and a menu screen class that loads all my texture atlases and skins for the menu and processes lots of stuff. If I put the constructor for the menu screen in the SplashScreen constructor or in the create() method of my main game class (MyGame class) then it would pass a lot of time with no splash screen render image on the phone while the whole stuff loads so I have delayed the loading of the menu class to the second frame render (gets called in the SplashScreen render method in the second frame); I simply check if I passed the first frame then I call a method on the main game class that loads the menus.

It all works fine , as expected , but only if the SplashScreen load process is not interrupted. If I get a call or I simple press the HOME button of the mobile and then I re-enter the game by clicking the icon on the home screen I get an AndroidGraphics error : "waiting for pause synchronization took too long: assuming dead lock and killing"; this is just after I can see the splash screen on the screen for about a second;

I tried to dispose the MyGame in the hide() and pause() methods of both the main game class and the splash screen class so it's terminated if I press the HOME button but they never get called.

How can I fix this? It would be annoying to get a call while loading the game and after you finish the call and try to re-enter the game it crashes.

public class MyGame extends Game{
    ...
    public MainMenu menu;
    ...
    @Override
    public void create(){ 
        this.screen_type == SCREEN_TYPE.SPLASH;
        splashScreen = new SplashScreen();
        setScreen(splashScreen);
    }
    ...
    @Override 
    public void pause(){
        //never gets called if I press the HOME button in middle of splash screen
        if(this.screen_type == SCREEN_TYPE.SPLASH)
        {
            this.dispose();
        }
    }
    ... 
    public void LoadMenuTimeConsumingConstructor(){
        //load all menus and process data
        main_menu = new MainMenu();
        loaded_menu = true;
    }
}

public class SplashScreen implements InputProcessor, Screen{

    public MyGame main_game;
    ...
    public SplashScreen(MyGame game){
        this.main_game = game;
    }

    @Override
    public void pause(){
        //never gets called if I press the HOME button in middle of splash screen
        if(main_game.screen_type == SCREEN_TYPE.SPLASH)
        {
            main_game.dispose();
        }
    }

    @Override 
    public void hide(){
        //never gets called if I press the HOME button in middle of splash screen
        if(main_game.screen_type == SCREEN_TYPE.SPLASH)
        {
            main_game.dispose();
        }
    }

    @Override 
    public void render(delta float){
        ...

        //wait 1.5 sec
        if(TimeUtils.millis() - startTime > 1500){
        {
            if(main_game.loaded_menu = true){
                main_game.setScreen(main_game.main_menu);
            }

        } 

        ...

        if(is_this_second_frame){  // we start loading menus in the second frame so we already have the splash onscreen
            main_game.LoadMenuTimeConsumingConstructor();
        }

        ...
    }
}

解决方案

I do not understand your question very well, but if you are not using AssetManger Class, I think this read can help, I hope so.

If this response is not valid or not useful to you, tell me, and delete

wiki LibGDX https://github.com/libgdx/libgdx/wiki/Managing-your-assets

video on YouTUBE https://www.youtube.com/watch?v=JXThbQir2gU

other example in GitHub https://github.com/Matsemann/libgdx-loading-screen

NEW look this:

Proper way to dispose screens in Libgdx

What's the right place to dispose a libgdx Screen

It can help you decide if you need to call dipose, and how, I hope it will help.

相关文章