【问题标题】:LibGDX Game Works as Expected on Desktop, But Not on Android DeviceLibGDX 游戏在桌面上按预期运行,但在 Android 设备上不正常
【发布时间】:2016-07-10 23:02:31
【问题描述】:

我遇到了一些问题。我对 libGDX 有点陌生,对出了什么问题一无所知。
在这里,我有一个包含三个文本按钮实例的状态。它在我的桌面上运行良好,但在我的手机上崩溃。我不确定问题出在哪里,但是当我注释掉其中两个实例的代码时,它可以在我的手机上运行。

public class MenuStateTest extends State {

Stage stage;
TextButton button, button2, button3;
TextButton.TextButtonStyle textButtonStyle;
BitmapFont font;
Skin skin;
TextureAtlas buttonAtlas, button2Atlas, button3Atlas;

public MenuStateTest(GameStateManager gsm) {
    super(gsm);
    stage = new Stage();
    Gdx.input.setInputProcessor(stage);
    font = new BitmapFont();
    skin = new Skin();

    buttonAtlas = new TextureAtlas("buttonTest.pack");
    skin.addRegions(buttonAtlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("1BBlock");
    textButtonStyle.down = skin.getDrawable("T1BBlock");
    button = new TextButton("", textButtonStyle);
    stage.addActor(button);
    button.setPosition(20,200);

    button2Atlas = new TextureAtlas("button2Test.pack");
    skin.addRegions(button2Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("2BBlock");
    textButtonStyle.down = skin.getDrawable("T2BBlock");
    button2 = new TextButton("", textButtonStyle);
    stage.addActor(button2);
    button2.setPosition(175,200);

    button3Atlas = new TextureAtlas("button3Test.pack");
    skin.addRegions(button3Atlas);
    textButtonStyle = new TextButton.TextButtonStyle();
    textButtonStyle.font = font;
    textButtonStyle.up = skin.getDrawable("3BBlock");
    textButtonStyle.down = skin.getDrawable("T3BBlock");
    button3 = new TextButton("", textButtonStyle);
    stage.addActor(button3);
    button3.setPosition(330,200);
}

@Override
protected void render(SpriteBatch sb) {
    stage.draw();

任何帮助将不胜感激。
谢谢。

编辑:这是我从 logcat 中得到的结果

03-23 11:44:54.510 25775-25799/? E/AndroidRuntime: FATAL EXCEPTION: GLThread 2379
                                               Process: com.mygame.game.android, PID: 25775
                                               Theme: themes:{default=overlay:com.blazze11.euphoria.dark, iconPack:system, fontPkg:system, com.android.systemui=overlay:com.blazze11.euphoria.dark, com.android.systemui.navbar=overlay:system}
                                               com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: button2Test.pack (Internal)
                                                   at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:77)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:211)
                                                   at com.mygame.game.States.MenuStateTest.<init>(MenuStateTest.java:41)
                                                   at com.mygame.game.MyGame.create(MyGame.java:29)
                                                   at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243)
                                                   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519)
                                                   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
                                                Caused by: java.io.FileNotFoundException: button2Test.pack
                                                   at android.content.res.AssetManager.openAsset(Native Method)
                                                   at android.content.res.AssetManager.open(AssetManager.java:331)
                                                   at android.content.res.AssetManager.open(AssetManager.java:305)
                                                   at com.badlogic.gdx.backends.android.AndroidFileHandle.read(AndroidFileHandle.java:75)
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:103) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:231) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:226) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:216) 
                                                   at com.badlogic.gdx.graphics.g2d.TextureAtlas.<init>(TextureAtlas.java:211) 
                                                   at com.mygame.game.States.MenuStateTest.<init>(MenuStateTest.java:41) 
                                                   at com.mygame.game.MyGame.create(MyGame.java:29) 
                                                   at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:243) 
                                                   at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1519) 
                                                   at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240) 

【问题讨论】:

  • 请提供错误的堆栈跟踪
  • 当我用 try and catch 块包围最后两个按钮实例时,代码在我的手机上运行。但只有第一个按钮被渲染到屏幕上。

标签: java android libgdx


【解决方案1】:

如果它运行在桌面上而不运行在Android上,我能想到的可能有两种:

  1. 检查包含button2Test.pack 的文件夹的路径 文件。 Android 路径是相对于 assets 文件夹的,而 desktop 使用相对于根文件夹的路径。
  2. 例如检查文件名button2Test.pack的大小写 是否应该是Button2Test.packbutton2test.PACK 或 类似的组合。因为 Windows 接受任何案例文件,但 Android 没有。

【讨论】:

  • 当然,是文件名。必须检查文件系统的正确名称。
猜你喜欢
  • 2015-02-08
  • 2023-03-30
  • 2021-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-27
  • 1970-01-01
  • 2020-06-02
相关资源
最近更新 更多