循环遍历2D数组时Android构建崩溃 - libGDX

时间:2015-06-03 19:48:06

标签: java android libgdx

我很快就会明白这一点 我正在开发我的小游戏,使用libgdx库,我有以下问题 每当我试图执行这个循环时:

for (int row = 0; row < currentMapHeightTiles; row++) {
        for (int col = 0; col < 9; col++) {
            if (currentMap[row][col] == 4 && (currentMapHeight - position.y) >  row * 40 && (currentMapHeight - position.y) < (row+1) * 40
                    && position.x > col * 40 && position.x < (col+1) * 40) {
                Gdx.app.log("Wall Collision", "found");
                collides = true;
            } else if (currentMap[row][col] == 3 && (currentMapHeight - position.y) >  row * 40 && (currentMapHeight - position.y) < (row+1) * 40
                    && position.x > col * 40 && position.x < (col+1) * 40) {
                Gdx.app.log("Gate Collision", "found");
                collides = true;
            }
        }
    }

我的android构建崩溃了。它就像桌面版上的魅力一样,甚至可以在Android上编译,但是一旦我执行它,我的手机就会说“我的应用程序停止工作”。

它的简化版本不起作用:

for (int row = 0; row < currentMapHeightTiles; row++) {
        for (int col = 0; col < 9; col++) {
            if (currentMap[row][col] == 4) {

            } else if (currentMap[row][col] == 3) {

            }
        }
    }

并且这个没有做任何事情,它只是遍历一个数组。

使用此方法创建2D数组:

public int[][] getMap() throws Exception {

    String line;

    Scanner scanner = new Scanner(new File("data/level_one.txt"));

        for (int row = 0; row < mapH; row++) {
            line = scanner.nextLine();
            String[] rowA = line.split(" ");
            for (int col = 0; col < 9; col++) {
                levelMap[row][col] = Integer.parseInt(rowA[col]);
            }
        }
    return levelMap;
}

日志:

    06-03 15:34:42.195    1293-1293/com.mestru.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:42.265    1293-1293/com.mestru.game.android D/dalvikvm﹕ Added shared lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:42.265    1293-1293/com.mestru.game.android D/dalvikvm﹕ No JNI_OnLoad found in /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8, skipping init
    06-03 15:34:42.425    1293-1293/com.mestru.game.android D/﹕ HostConnection::get() New Host Connection established 0xb8466920, tid 1293
    06-03 15:34:42.965    1293-1293/com.mestru.game.android I/AndroidInput﹕ sensor listener setup
    06-03 15:34:43.215    1293-1293/com.mestru.game.android W/EGL_emulation﹕ eglSurfaceAttrib not implemented
    06-03 15:34:43.235    1293-1293/com.mestru.game.android D/OpenGLRenderer﹕ Enabling debug mode 0
    06-03 15:34:43.385    1293-1306/com.mestru.game.android D/﹕ HostConnection::get() New Host Connection established 0xb85aba30, tid 1306
    06-03 15:34:43.425    1293-1306/com.mestru.game.android W/GL2JNIView﹕ creating OpenGL ES 2.0 context
    06-03 15:34:43.545    1293-1306/com.mestru.game.android D/dalvikvm﹕ Trying to load lib /data/app-lib/com.mestru.game.android-1/libgdx.so 0xb1db73c8
    06-03 15:34:43.545    1293-1306/com.mestru.game.android D/dalvikvm﹕ Shared lib '/data/app-lib/com.mestru.game.android-1/libgdx.so' already loaded in same CL 0xb1db73c8
    06-03 15:34:43.555    1293-1306/com.mestru.game.android I/GL2﹕ all initialized 2
    06-03 15:34:43.565    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL renderer: Android Emulator OpenGL ES Translator (AMD Radeon R9 200 Series)
    06-03 15:34:43.565    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL vendor: Google (ATI Technologies Inc.)
    06-03 15:34:43.575    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL version: OpenGL ES 2.0 (4.4.13283 Compatibility Profile Context 14.501.1003.0)
    06-03 15:34:43.625    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ OGL extensions: GL_EXT_debug_marker GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_depth24 GL_OES_depth32 GL_OES_element_index_uint GL_OES_texture_float GL_OES_texture_float_linear GL_OES_compressed_paletted_texture GL_OES_compressed_ETC1_RGB8_texture GL_OES_depth_texture GL_OES_texture_half_float GL_OES_texture_half_float_linear GL_OES_packed_depth_stencil GL_OES_vertex_half_float GL_OES_texture_npot GL_OES_rgb8_rgba8
    06-03 15:34:43.625    1293-1306/com.mestru.game.android E/EGL_emulation﹕ [getAttribValue] Bad attribute idx
    06-03 15:34:43.625    1293-1306/com.mestru.game.android E/EGL_emulation﹕ tid 1306: eglGetConfigAttrib(605): error 0x3004 (EGL_BAD_ATTRIBUTE)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android E/EGL_emulation﹕ [getAttribValue] Bad attribute idx
    06-03 15:34:43.635    1293-1306/com.mestru.game.android E/EGL_emulation﹕ tid 1306: eglGetConfigAttrib(605): error 0x3004 (EGL_BAD_ATTRIBUTE)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ framebuffer: (5, 6, 5, 0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ depthbuffer: (16)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ stencilbuffer: (0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ samples: (0)
    06-03 15:34:43.635    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ coverage sampling: (false)
    06-03 15:34:43.675    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed meshes/app: { }
    06-03 15:34:43.675    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed textures/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed cubemap/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed shaders/app: { }
    06-03 15:34:43.685    1293-1306/com.mestru.game.android I/AndroidGraphics﹕ Managed buffers/app: { }
    06-03 15:34:43.695    1293-1306/com.mestru.game.android I/MathsGame﹕ created
    06-03 15:34:44.185    1293-1306/com.mestru.game.android W/System.err﹕ java.io.FileNotFoundException: /data/level_one.txt: open failed: ENOENT (No such file or directory)
    06-03 15:34:44.185    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:409)
    06-03 15:34:44.285    1293-1306/com.mestru.game.android W/System.err﹕ at java.io.FileInputStream.<init>(FileInputStream.java:78)
    06-03 15:34:44.285    1293-1306/com.mestru.game.android W/System.err﹕ at java.util.Scanner.<init>(Scanner.java:158)
    06-03 15:34:44.305    1293-1306/com.mestru.game.android W/System.err﹕ at java.util.Scanner.<init>(Scanner.java:138)
    06-03 15:34:44.305    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.screens.levels.Level_One.getMap(Level_One.java:24)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.gameWorld.GameWorld.initLevels(GameWorld.java:45)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.gameWorld.GameWorld.<init>(GameWorld.java:35)
    06-03 15:34:44.315    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.screens.GameScreen.<init>(GameScreen.java:19)
    06-03 15:34:44.425    1293-1306/com.mestru.game.android W/System.err﹕ at com.mestru.game.MathsGame.create(MathsGame.java:14)
    06-03 15:34:44.425    1293-1306/com.mestru.game.android W/System.err﹕ at com.badlogic.gdx.backends.android.AndroidGraphics.onSurfaceChanged(AndroidGraphics.java:241)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1512)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.Posix.open(Native Method)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ at libcore.io.IoBridge.open(IoBridge.java:393)
    06-03 15:34:44.435    1293-1306/com.mestru.game.android W/System.err﹕ ... 11 more
    06-03 15:34:44.885    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 232K, 9% free 3153K/3452K, paused 50ms, total 53ms
    06-03 15:34:46.105    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 393K, 13% free 3253K/3716K, paused 41ms, total 42ms
    06-03 15:34:46.525    1293-1306/com.mestru.game.android D/dalvikvm﹕ GC_FOR_ALLOC freed 67K, 11% free 3402K/3796K, paused 40ms, total 40ms
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ show called
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ resize called
    06-03 15:34:46.565    1293-1306/com.mestru.game.android I/GameScreen﹕ resize called
    06-03 15:34:54.915    1293-1306/com.mestru.game.android W/dalvikvm﹕ threadid=11: thread exiting with uncaught exception (group=0xb1ad9ba8)
    06-03 15:34:54.925    1293-1306/com.mestru.game.android E/AndroidRuntime﹕ FATAL EXCEPTION: GLThread 93
        Process: com.mestru.game.android, PID: 1293
        java.lang.NullPointerException
            at com.mestru.objects.Player.checkCollision(Player.java:143)
            at com.mestru.objects.Player.update(Player.java:56)
            at com.mestru.gameWorld.GameWorld.update_one(GameWorld.java:83)
            at com.mestru.gameWorld.GameWorld.update(GameWorld.java:71)
            at com.mestru.screens.GameScreen.render(GameScreen.java:32)
            at com.badlogic.gdx.Game.render(Game.java:46)
            at com.badlogic.gdx.backends.android.AndroidGraphics.onDrawFrame(AndroidGraphics.java:422)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1523)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1240)
    06-03 15:34:58.145    1293-1306/com.mestru.game.android I/Process﹕ Sending signal. PID: 1293 SIG: 9

所以是的,conclusing - 每次我通过这个数组,我的android构建崩溃。当我尝试使用try / catch时,它根本不检查碰撞。会使用任何帮助&lt; 3

编辑。仍然无法让它发挥作用:&lt;

EDIT2。幸运的是,在我的帮助下,我设法让它发挥作用。 我已经取代了

 Scanner scanner = new Scanner(new File("data/level_one.txt"));

FileHandle file = Gdx.files.internal("data/level_one.txt");

Scanner scanner = new Scanner(file.readString());

现在它就像魅力一样:)

1 个答案:

答案 0 :(得分:1)

在我的代码的这一行中,我认为是一个问题 - &gt;

public int[][] getMap() throws Exception {

    String line;

    Scanner scanner = new Scanner(new File("data/level_one.txt"));

        for (int row = 0; row < mapH; row++) {
            line = scanner.nextLine();
            String[] rowA = line.split(" ");
            for (int col = 0; col < 9; col++) {
                levelMap[row][col] = Integer.parseInt(rowA[col]);
            }
        }
    return levelMap;
}

  

扫描仪扫描仪=新扫描仪(新文件(&#34; data / level_one.txt&#34;));

示例:

Scanner scanner = new Scanner(Gdx.files.internal("data/level_one.txt").file());

阅读此内容并在您的代码中实施:

https://github.com/libgdx/libgdx/wiki/File-handling#reading-from-a-file

https://github.com/libgdx/libgdx/wiki/File-handling#obtaining-filehandles

也许这也会影响您的代码,我希望它会有所帮助,如果没有,请通知我并删除回复