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

标签 java android-activity libgdx scene2d

我有一个启动画面和一个菜单屏幕类,它们加载我所有的纹理图集和菜单的皮肤并处理很多东西。如果我将菜单屏幕的构造函数放在 SplashScreen 构造函数 或我的主游戏类(MyGame 类)的 create() 方法中,那么它会传递很多加载所有内容时手机上没有启动画面渲染图像的时间,因此我将菜单类的加载延迟到第二帧渲染(在第二帧的 SplashScreen 渲染方法中调用);我只是检查我是否通过了第一帧,然后我在主游戏类上调用一个方法来加载菜单。

一切正常,正如预期的那样,但前提是 SplashScreen 加载过程没有中断。如果我接到一个电话,或者我只是按下手机的主屏幕按钮,然后通过单击主屏幕上的图标重新进入游戏,我会收到一个 AndroidGraphics 错误:“waiting for暂停同步时间过长:假设死锁并杀死”;这是在我能在屏幕上看到启动画面大约一秒钟之后;

我试图在主游戏类和启动画面类的 hide() 和 pause() 方法中配置 MyGame,因此如果我按下 HOME 按钮,它就会终止,但它们永远不会被调用。

我该如何解决这个问题?在加载游戏时接到电话,在您结束通话并尝试重新进入游戏后它崩溃了,这会很烦人。

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

        ...
    }
}

最佳答案

我不是很理解你的问题,但如果你没有使用 AssetManger 类,我认为这篇文章会有所帮助,我希望如此。

如果此回复无效或对您没有用,请告诉我,然后删除

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

YouTube 上的视频 https://www.youtube.com/watch?v=JXThbQir2gU

GitHub 中的其他示例 https://github.com/Matsemann/libgdx-loading-screen

看这个:

Proper way to dispose screens in Libgdx

What's the right place to dispose a libgdx Screen

它可以帮助您决定是否需要调用 dipose,以及如何调用,希望对您有所帮助。

关于java - 如果启动画面打开时中断,游戏会崩溃 - LIBGDX,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30119243/

相关文章:

android - 使用静态方法创建 AlertDialog?

java - 如何获取 Box2D 多边形的坐标?

java - 使用 Spring Boot 和 Thymeleaf 创建文件下载链接

java - Checkstyle Java 泛型 : '?' is not preceded with whitespace

java - 使用 java-mongo 驱动程序的反射时出现问题

java - 如何在云端模拟批量运行?

java - 切换运行StartActivity时出错

android - 应用程序正在运行但 MAP 是空白的我正在使用 google map api 来定位某个位置

android - 带有正交相机的 libgdx 视口(viewport)段

java - libgdx 某些按键比其他按键快?