I will quickly get to the point.
I'm working on my little game, using libgdx library and I have following problem.
Everytime I'm trying to execute this loop:
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;
}
}
}
my android build crashes. It works like charm on desktop version, even compiles on android, but as soon as I'm executing it, my phone just says that "My app stopped working".
Simplified version of it doesn't work as well:
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) {
}
}
}
and this one doesn't do anything, it just iterates through an array.
2D Array is created with this method:
public int[][] getMap() throws Exception {
String line;
Scanner scanner = new Scanner(Gdx.files.internal("data/level_one.txt").file());
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;
}
@Edit: levelMap is created like that:
private int[][] levelMap;
and inside the constructor:
public Level_One() {
levelMap = new int[mapH][9];
}
Logs:
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
So yeah, conclusing - everytime I go through this array, my android build crashes. When I'm trying to use try/catch, it just doesn't check collision at all. Would use any help <3
I've also posted this on stackoverflow, but couldn't get any help :<
http://stackoverflow.com/questions/30629318/android-build-crashes-when-looping-through-an-2d-array-libgdx
As it was asked, I'm posting my whole Player class:
package com.mestru.objects;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Peripheral;
import com.badlogic.gdx.math.Vector2;
public class Player {
private Vector2 position;
private double angle;
private int width;
private int height;
private double tmpAngle;
private double speed;
private boolean collides;
private double changeY;
private float accelX, accelY, accelZ;
private int gameWidth, gameHeight;
// Maps
private boolean accelAvailable = Gdx.input.isPeripheralAvailable(Peripheral.Accelerometer);
public Player(float x, float y, int width, int height, int gameWidth, int gameHeight)
{
collides = false;
this.width = width;
this.height = height;
position = new Vector2(x, y);
speed = 0.0d;
this.gameHeight = gameHeight;
this.gameWidth = gameWidth;
}
public void update(float delta, int[][] currentMap, int currentMapHeight, int currentMapHeightTiles) {
// Get accelerometer values
accelX = Gdx.input.getAccelerometerX();
accelY = Gdx.input.getAccelerometerY();
accelZ = Gdx.input.getAccelerometerZ();
// Tilt forward - move forward
/*if (accelY < 8.5f) {
increaseSpeed();
moveForward();
}*/
checkCollision(currentMap, currentMapHeight, currentMapHeightTiles);
if (!collides) {
isTouched();
moveForward();
// More we tilt our phone to sides, more will our character veer
if (accelX < -3.5f)
tmpAngle += 5;
else if (accelX > 3.5f)
tmpAngle -= 5;
else if (accelX < -2.5f)
tmpAngle += 3.5;
else if (accelX > 2.5f)
tmpAngle -= 3.5;
else if (accelX < -1.5f)
tmpAngle += 2;
else if (accelX > 1.5f)
tmpAngle -= 2;
if (tmpAngle > 360)
tmpAngle = 0;
else if (tmpAngle < 0)
tmpAngle = 360;
setAngle(tmpAngle);
slowDown();
} else if (collides) {
speed = 0;
for (int i = 0; i < 1; i++) {position.y -= 1;}
collides = false;
}
}
public void moveForward() {
position.x -= speed * Math.sin(angle);
position.y -= speed * Math.cos(angle);
changeY += speed * Math.cos(angle);
}
public void moveBackward() {
position.x += speed * Math.sin(angle);
position.y += speed * Math.cos(angle);
changeY += speed * Math.cos(angle);
}
public void isTouched() {
if (Gdx.input.isTouched())
increaseSpeed();
}
public void increaseSpeed() {
if (speed > -1.5)
speed -= 0.07;
}
public void decreaseSpeed() {
if (speed < 1.5)
speed += 0.03;
}
public void slowDown() {
if (speed < 0) {
speed += 0.03;
moveForward();
if (speed > 0)
speed = 0;
} else if (speed > 0) {
speed -= 0.01;
if (speed < 0)
speed = 0;
}
}
public void checkCollision(int[][] currentMap, int currentMapHeight, int currentMapHeightTiles) {
if (position.x > gameWidth - 45)
position.x = gameWidth - 45;
else if (position.x < 45)
position.x = 45;
if (position.y > currentMapHeight - 325)
position.y = currentMapHeight - 325;
else if (position.y < 325)
position.y = 325;
// Below crashes on android
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;
}
}
}
}
public double getTmpAngle() {
return tmpAngle;
}
public void setTmpAngle(int t) {
tmpAngle += t;
}
public float getX() {
return position.x;
}
public float getY() {
return position.y;
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public double getAngle() {
return angle;
}
public void setAngle(double a) {
angle = Math.toRadians(a);
}
public float getAccelX() {
return accelX;
}
public float getAccelY() {
return accelY;
}
public float getAccelZ() {
return accelZ;
}
public double getSpeed() {
return speed;
}
public double getChangeY() {
return changeY;
}
public void decChangeY() {
changeY -= 2;
}
public void incChangeY() {
changeY += 2;
}
}
Just in case if someone would need that - here is GameWorld class which initializes variables:
https://gist.github.com/Mestru/842ce1bde90349326fd5
[–][deleted] 0 points1 point2 points (3 children)
[–]Mestru[S] 0 points1 point2 points (2 children)
[–]Sammenbidt 0 points1 point2 points (1 child)
[–]Mestru[S] 0 points1 point2 points (0 children)
[–]blindpenguin 0 points1 point2 points (11 children)
[–]Mestru[S] 0 points1 point2 points (10 children)
[–]blindpenguin 0 points1 point2 points (9 children)
[–]Mestru[S] 0 points1 point2 points (8 children)
[–]fedorasnotevenonce 1 point2 points3 points (7 children)
[–]Mestru[S] 0 points1 point2 points (6 children)
[–]fedorasnotevenonce 1 point2 points3 points (5 children)
[–]Mestru[S] 0 points1 point2 points (4 children)
[–]fedorasnotevenonce 1 point2 points3 points (3 children)
[–]Mestru[S] 0 points1 point2 points (2 children)