getDeltaTime()方法给我null异常?

时间:2017-06-16 22:58:53

标签: java libgdx tiled

出于某种原因,当我尝试将Gdx.graphics.getDeltaTime()传递到我的播放器类时,浮点增量时间为空,给出空指针异常。我已经尝试检查getDeltaTime方法是否有效,以及它的计数,但是当我尝试通过该函数时它不起作用。

我的主要代码

     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();

}

我的玩家类的一部分

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;
    }
}

2 个答案:

答案 0 :(得分:0)

只是为了排除delta代码重构,你可以像这样编码: -

@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);
  .......
  .......
}

答案 1 :(得分:0)

听起来像你的播放器类update()方法是问题,但不是浮动增量。看起来像是速度。它是空的。

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

您正在尝试设置速度的y公共浮点值(Vector2),但您没有实例化速度对象。

尝试

 private Vector2 velocity = new Vector2();