I am creating a Game in java, using LibGdx. It worked perfectly fine when compiled until I added Layers to my Tiled Map. It still runs fine when ran from Eclipse but when i compile it into a runnable jar, it does not even open. I commented out everything to do with Layers and it ran perfect again, so i have no idea what is causing this issue.
Here is the Stack:
Exception in thread "LWJGL Application" java.lang.NullPointerException at com.mygdx.game.Game_Screen.render(Game_Screen.java:181) at com.badlogic.gdx.Game.render(Game.java:46) at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:215) at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:120)
And here is the code:
package com.mygdx.game;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.maps.tiled.TiledMap;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer;
import com.badlogic.gdx.maps.tiled.TiledMapTileLayer.Cell;
import com.badlogic.gdx.maps.tiled.TmxMapLoader;
import com.badlogic.gdx.maps.tiled.renderers.OrthogonalTiledMapRenderer;
public class Game_Screen implements Screen{
private TiledMap map;
private OrthogonalTiledMapRenderer renderer;
OrthographicCamera camera;
private static final int FRAME_COLS = 4; // #1
private static final int FRAME_ROWS = 4; // #2
Animation walkAnimationUp; // #3
Animation walkAnimationUpIdle; // #3
Animation walkAnimationDown; // #3
Animation walkAnimationDownIdle; // #3
Animation walkAnimationLeft; // #3
Animation walkAnimationLeftIdle; // #3
Animation walkAnimationRight; // #3
Animation walkAnimationRightIdle; // #3
Cell cellRight;
int blockedRight = 0;
Cell cellLeft;
int blockedLeft = 0;
Cell cellUp;
int blockedUp = 1;
Cell cellDown;
int blockedDown = 1;
int cellX;
int cellY;
int[] layers = {1,2,3};
Texture walkSheet; // #4
TextureRegion[] walkFramesUp; // #5
TextureRegion[] walkFramesUpIdle; // #5
TextureRegion[] walkFramesDown; // #5
TextureRegion[] walkFramesDownIdle; // #5
TextureRegion[] walkFramesLeft; // #5
TextureRegion[] walkFramesLeftIdle; // #5
TextureRegion[] walkFramesRight;
TextureRegion[] walkFramesRightIdle;
int boundaryXLeft = 0;
int boundaryYDown = 0;
int boundaryXRight = 6720;
int boundaryYUp = 4032;
MyGdxGame game;
SpriteBatch spriteBatch; // #6
TextureRegion currentFrame; // #7
int spriteX = 0;
int spriteY = 0;
int newX = 0;
int newY = 0;
int LastKey = 0;
int CurrentKey = 0;
int speed;
float stateTime; // #8
boolean pause = false;
TiledMapTileLayer layer;
String collisionLayer;
public Game_Screen(MyGdxGame game) {
this.game = game;
TmxMapLoader loader = new TmxMapLoader();
map = loader.load("ActualMap.tmx");
renderer = new OrthogonalTiledMapRenderer(map);
camera = new OrthographicCamera();
camera.setToOrtho(false, 1920, 1080);
camera.position.set(spriteX,spriteY, stateTime);
walkSheet = new Texture(Gdx.files.internal("newSprite.png")); // #9
TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS); // #10
walkFramesRight = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesRightIdle = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesLeft = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesLeftIdle = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesUp = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesUpIdle = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesDown = new TextureRegion[FRAME_COLS * FRAME_ROWS];
walkFramesDownIdle = new TextureRegion[FRAME_COLS * FRAME_ROWS];
collisionLayer = "collision";
speed = 2;
;
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesRight[index++] = tmp[2][j];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesRightIdle[index++] = tmp[2][1];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesLeft[index++] = tmp[1][j];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesLeftIdle[index++] = tmp[1][1];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesUp[index++] = tmp[3][j];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesUpIdle[index++] = tmp[3][1];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesDown[index++] = tmp[0][j];
}
}
index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFramesDownIdle[index++] = tmp[0][1];
}
}
walkAnimationRight = new Animation(0.2f, walkFramesRight); // #11
walkAnimationRightIdle = new Animation(0.2f, walkFramesRightIdle); // #11
walkAnimationLeft = new Animation(0.2f, walkFramesLeft); // #11
walkAnimationLeftIdle = new Animation(0.2f, walkFramesLeftIdle); // #11
walkAnimationUp = new Animation(0.2f, walkFramesUp); // #11
walkAnimationUpIdle = new Animation(0.2f, walkFramesUpIdle); // #11
walkAnimationDown = new Animation(0.2f, walkFramesDown); // #11
walkAnimationDownIdle = new Animation(0.2f, walkFramesDownIdle); // #11
spriteBatch = new SpriteBatch(); // #12
stateTime = 0f; // #13
}
@Override
public void render(float delta) {
layer = (TiledMapTileLayer) map.getLayers().get(collisionLayer);
camera.update();
spriteBatch.setProjectionMatrix(camera.combined);
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); // #14
stateTime += Gdx.graphics.getDeltaTime(); // #15
cellX = spriteX/64;
cellY = spriteY/64;
cellUp = layer.getCell(cellX, cellY+1);
cellDown = layer.getCell(cellX, cellY-1);
cellRight = layer.getCell(cellX+1, cellY);
cellLeft = layer.getCell(cellX-1, cellY);
blockedUp = Integer.parseInt((String) cellUp.getTile().getProperties().get("blocked"));
if(cellY != 0)
blockedDown = Integer.parseInt((String) cellDown.getTile().getProperties().get("blocked"));
if(cellX != 0)
blockedLeft = Integer.parseInt((String) cellLeft.getTile().getProperties().get("blocked"));
blockedRight = Integer.parseInt((String) cellRight.getTile().getProperties().get("blocked"));
if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && spriteX == newX && spriteY == newY && spriteX > boundaryXLeft && blockedLeft == 0)
{
LastKey = 1;
newX-=64;
CurrentKey = 1;
}
else if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) && spriteX == newX && spriteY == newY && spriteX < boundaryXRight && blockedRight == 0 )
{
LastKey = 0;
newX+=64;
CurrentKey = 0;
}
else if(Gdx.input.isKeyPressed(Input.Keys.UP) && spriteY == newY && spriteX == newX && spriteY < boundaryYUp && blockedUp == 0)
{
LastKey = 2;
newY+=64;
CurrentKey = 2;
}
else if(Gdx.input.isKeyPressed(Input.Keys.DOWN) && spriteY == newY && spriteX == newX && spriteY > boundaryYDown && blockedDown == 0 )
{
LastKey = 3;
newY-=64;
CurrentKey = 3;
}
else if(spriteX == newX && spriteY == newY)
{
CurrentKey = 4;
}
if(Gdx.input.isKeyPressed(Input.Keys.DOWN) && spriteY == newY && spriteX == newX && spriteY > boundaryYDown && blockedDown == 2 && LastKey == 3)
{
LastKey = 3;
newY-=128;
CurrentKey = 3;
if(collisionLayer.equals("collision"))
{
collisionLayer = "Inside";
layers[1] = 4;
layers[2] = 4;
}
else if(collisionLayer.equals("Inside"))
{
collisionLayer = "collision";
layers[1] = 2;
layers[2] = 3;
}
}
if(Gdx.input.isKeyPressed(Input.Keys.UP) && spriteY == newY && spriteX == newX && spriteY < boundaryYUp && blockedUp == 2 && LastKey == 2)
{
LastKey = 2;
newY+=128;
CurrentKey = 2;
if(collisionLayer.equals("collision"))
{
collisionLayer = "Inside";
layers[1] = 4;
layers[2] = 4;
}
else if(collisionLayer.equals("Inside"))
{
collisionLayer = "collision";
layers[1] = 2;
layers[2] = 3;
}
}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT) && LastKey != 1 && spriteY == newY && spriteX == newX )
{
LastKey = 1;
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT) && LastKey != 0 && spriteY == newY && spriteX == newX )
{
LastKey = 0;
}
if(Gdx.input.isKeyPressed(Input.Keys.UP) && LastKey != 2 && spriteY == newY && spriteX == newX )
{
LastKey = 2;
}
if(Gdx.input.isKeyPressed(Input.Keys.DOWN) && LastKey != 3 && spriteY == newY && spriteX == newX )
{
LastKey = 3;
}
if(Gdx.input.isKeyJustPressed(Input.Keys.ESCAPE))
{
Gdx.app.exit();
}
if(spriteX < newX)
spriteX+=(speed);
if(spriteX > newX)
spriteX-=(speed);
if(spriteY < newY)
spriteY+=(speed);
if(spriteY > newY)
spriteY-=(speed);
if(LastKey == 0 && spriteX != newX)
{;
currentFrame = walkAnimationRight.getKeyFrame(stateTime, true); // #16
}
else if(LastKey == 0 && spriteX == newX && CurrentKey == 4)
{
currentFrame = walkAnimationRightIdle.getKeyFrame(stateTime, true); // #16
}
if(LastKey == 1 && spriteX != newX)
{
currentFrame = walkAnimationLeft.getKeyFrame(stateTime, true); // #16
}
else if (LastKey == 1 && spriteX == newX && CurrentKey == 4)
{
currentFrame = walkAnimationLeftIdle.getKeyFrame(stateTime, true); // #16
}
if(LastKey == 2 && spriteY != newY)
{
currentFrame = walkAnimationUp.getKeyFrame(stateTime, true); // #16
}
else if (LastKey == 2 && spriteY == newY && CurrentKey == 4)
{
currentFrame = walkAnimationUpIdle.getKeyFrame(stateTime, true); // #16
}
if(LastKey == 3 && spriteY != newY)
{
currentFrame = walkAnimationDown.getKeyFrame(stateTime, true); // #16
}
else if (LastKey == 3 && spriteY == newY && CurrentKey == 4)
{
currentFrame = walkAnimationDownIdle.getKeyFrame(stateTime, true); // #16
}
camera.position.set(spriteX, spriteY, 0);
renderer.setView(camera);
renderer.render(layers);;
spriteBatch.begin();
spriteBatch.draw(currentFrame, spriteX, spriteY); // #17
spriteBatch.end();
}
@Override
public void dispose() {
// TODO Auto-generated method stub
spriteBatch.dispose();
renderer.dispose();
map.dispose();
}
@Override
public void resize(int width, int height) {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void show() {
// TODO Auto-generated method stub
}
}