0

For some reason, when I try to pass through Gdx.graphics.getDeltaTime() into my player class, the float delta time is null giving a null pointer exception. I already tried to check if the getDeltaTime method worked, and its counting, but when i try to pass through the function it doesn't work.

My Main Code

     public class MyGdxGame extends ApplicationAdapter implements InputProcessor {

SpriteBatch batch;
private Player1 player1;
private TiledMap iceLevel;
private OrthogonalTiledMapRenderer renderer;
private OrthographicCamera camera;
float elaspedTime;

@Override
public void create() {
    batch = new SpriteBatch();
    Gdx.input.setInputProcessor(this);

    TmxMapLoader loader = new TmxMapLoader();
    iceLevel = loader.load("IceLevel.tmx");
    renderer = new OrthogonalTiledMapRenderer(iceLevel);
    camera = new OrthographicCamera();
    camera.setToOrtho(false);

    player1 = new Player1(new Sprite(new Texture("player1Right.png")), (TiledMapTileLayer) iceLevel.getLayers().get("Land"));
    player1.setPosition(player1.getCollisionLayer().getTileWidth(), 6 * player1.getCollisionLayer().getTileHeight());
}

@Override
public void render() {
    player1.update(Gdx.graphics.getDeltaTime());

    Gdx.gl.glClearColor(1, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();

    camera.update();
    renderer.setView(camera);
    renderer.render();

    renderer.getBatch().begin();

    player1.draw(renderer.getBatch());

    renderer.getBatch().end();

    batch.end();
}

@Override
public void dispose() {
    batch.dispose();
    iceLevel.dispose();
    renderer.dispose();
    player1.getTexture().dispose();

}

A part of my player class

public class Player1 extends Sprite implements InputProcessor {

private Vector2 velocity;

private float speed = 60 * 2;
private float gravity = 60 * 1.8f;
private TiledMapTileLayer collisionLayer;

public Player1(Sprite sprite, TiledMapTileLayer collisionLayer) {
    super(sprite);
    this.collisionLayer = collisionLayer;
}

public void draw(SpriteBatch spriteBatch) {
    update(Gdx.graphics.getDeltaTime());
    super.draw(spriteBatch);
}

public void update(float delta) {

    //gravity
    velocity.y -= gravity * delta;

    //set limit
    if (velocity.y > speed) {
        velocity.y = speed;

    } else if (velocity.y < -speed) {
        velocity.y = -speed;
    }

    // save old position
    float oldX = getX();
    float oldY = getY();
    boolean collisionX = false;
    boolean collisionY = false;

    // move on x
    setX(getX() + velocity.x * delta);

    if (velocity.x < 0) { //going left
        collisionX = collidesLeft();
    } else if (velocity.x > 0) { //going right
        collisionX = collidesRight();
    }

    // react to x collision
    if (collisionX) {
        setX(oldX);
        velocity.x = 0;
    }
    // move on y
    setY(getY() + velocity.y * delta * 5f);

    if (velocity.y < 0) { // going down
        collisionY = collidesBottom();
    } else if (velocity.y > 0) { // going up

        collisionY = collidesTop();
    }
    if (collisionY) {
        setY(oldY);
        velocity.y = 0;
    }
}
  • 1
    Floats cannot be null. The null pointer exception is probably because you haven't initialized the graphics or that you accidentally made it null. Are you launching the game using the default desktop launcher? Also can we please see your stack trace? – Charanor Jun 16 '17 at 23:03

2 Answers2

0

Just to rule out delta time refactor you code like this:-

@Override
public void render() {
float delta = Gdx.graphics.getDeltaTime();
// Log or print this
if(delta > 0){
  player1.update(Gdx.graphics.getDeltaTime());

  Gdx.gl.glClearColor(1, 0, 0, 1);
  .......
  .......
}
Zakir
  • 2,222
  • 21
  • 31
0

Sounds like your player class update() method is the issue, but not the float delta. Looks like it is velocity. It is null.

private Vector2 velocity;
...
//gravity
velocity.y -= gravity * delta;

You are trying to set the y public float value of velocity (Vector2) but you don't have a velocity object instantiated.

Try

 private Vector2 velocity = new Vector2();
Peter R
  • 1,034
  • 8
  • 12