HTML部署中的LibGDX GwtApplication Exception(TypeError)

时间:2018-06-13 10:36:36

标签: java html5 gwt libgdx

我尝试用HTML部署我的libgdx游戏。在桌面和Android中它运行良好。当我做./gradlew html:dist时,它编译得很好,我有一个在html文件夹中创建的dist文件夹。我成功地在浏览器上启动游戏: enter image description here

然而,当我点击任何会改变屏幕的按钮时,游戏崩溃: enter image description here

错误是:

  

GwtApplication:exception:(TypeError):a.g [a.b]未定义   (TypeError):a.g [a.b]未定义

在控制台中,我有:

  

错误:java.lang.RuntimeException:com.google.gwt.core.client.JavaScriptException:(TypeError):a.g [a.b]未定义

两个问题(12)有同样的问题,但这些都没有解决我的问题。

----- 以下是我改变屏幕的方式 ----

当用户点击Stats例如时,我的代码会这样做:

button_stats.addListener(new ClickListener(){
        @Override
        public void clicked(InputEvent event, float x, float y){

            stage.addAction(Actions.sequence(Actions.fadeOut(0.2f), Actions.run(new Runnable(){
                @Override
                public void run() {
                    ScreenManager.getInstance().showScreen(ScreenEnum.STATS);
                    }
            })));
        }
    });

这是我的屏幕管理员:

public class ScreenManager {

    // Singleton: unique instance
    private static ScreenManager instance;

    // Reference to game
    private SpeedRun2Game game;

    // Singleton: private constructor
    private ScreenManager() {
        super();
    }

    // Singleton: retrieve instance
    public static ScreenManager getInstance() {
        if (instance == null) {
            instance = new ScreenManager();
        }
        return instance;
    }

    // Initialization with the game class
    public void initialize(SpeedRun2Game game) {
        this.game = game;
    }

    // Show in the game the screen which enum type is received
    public void showScreen(com.gangscred.speedrun2.screens.ScreenEnum screenEnum, Object... params) {

        // Get current screen to dispose it
        Screen currentScreen = game.getScreen();

        // Show new screen
        Screen newScreen = screenEnum.getScreen(game , params);
        game.setScreen(newScreen);

        // Dispose previous screen
        if (currentScreen != null) {
            currentScreen.dispose();
        }
    }
}

最后我的screenEnum:

public enum ScreenEnum {
STATS{
  public Screen getScreen(SpeedRun2Game game, Object... params){
      return new StatsScreen(game);
  }
}

----- 编辑 -----

我在我的gwt编译器中添加了style ='PRETTY'选项,现在我有这个错误:

  

错误:java.lang.RuntimeException:com.google.gwt.core.client.JavaScriptException:(TypeError):此$ static.uniforms [this $ static.currProgram]未定义

1 个答案:

答案 0 :(得分:2)

出于某种原因,在HTML中,您无法在新的Runnable中使用静态方法。因此,当您想要更改屏幕时,需要删除淡出:

button_stats.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y){
         ScreenManager.getInstance().showScreen(ScreenEnum.STATS);
    }
});

或者您可以保持淡出但删除静态功能并直接更改屏幕:

SpeedRun2Game game;
...
button_stats.addListener(new ClickListener(){
    @Override
    public void clicked(InputEvent event, float x, float y){

        stage.addAction(Actions.sequence(Actions.fadeOut(0.2f), Actions.run(new Runnable(){
            @Override
            public void run() {
                game.setScreen(new StatsScreen(game));
                }
        })));
    }
});