Search code examples
javaandroidanimationlibgdx2d

LibGDX - Trying to animate a sprite, crashes my whole game


I'm working on an Android game and so far the way I draw game objects is I initialise them in Game then put them in an array list of GameObject (every object extends this abstract class; player, flag, coin). This array list gets passed into Renderer which then draws the objects using a for loop (that iterates over the array list).

I am trying to add a new GameObject called Coin. Unlike other objects I want this one to be animated, and already have 8 pictures representing each frame of the animation. Here's my code (using the libgdx Animation class):

public class Coin extends GameObject implements Screen {
private SpriteBatch batch;
private Animation animation;
private float time;

public Coin(Sprite spr, float xPos, float yPos,
        float radius) {
    super(spr, xPos, yPos, radius);
    setxPos(xPos);
    setyPos(yPos);
    batch = new SpriteBatch();
    time = 0;
}

public void render(float delta) {
// TODO Auto-generated method stub
batch.begin();
batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());
batch.end();
}


@Override
public void resize(int width, int height) {
    // TODO Auto-generated method stub

}

@Override
public void show() {
    // TODO Auto-generated method stub
    //batch = new SpriteBatch();
    animation = new Animation(1 / 3f, 
            new TextureRegion(new Texture("coin1.png")), 
            new TextureRegion(new Texture("coin2.png")),
            new TextureRegion(new Texture("coin3.png")), 
            new TextureRegion(new Texture("coin4.png")), 
            new TextureRegion(new Texture("coin5.png")), 
            new TextureRegion(new Texture("coin6.png")), 
            new TextureRegion(new Texture("coin7.png")), 
            new TextureRegion(new Texture("coin8.png")));
    animation.setPlayMode(Animation.PlayMode.LOOP);
}
@Override
public void hide() {
    // 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 dispose() {
    // TODO Auto-generated method stub
    batch.dispose();
}

}

The error I get from the LogCAT is a NullPointerException @ batch.draw(animation.getKeyFrame(time += delta), getxPos(), getyPos());

Does anyone know how to fix this? Any insight would be highly appreciated


Solution

  • Write this code:

    public Coin(Sprite spr, float xPos, float yPos,
        float radius) {
    super(spr, xPos, yPos, radius);
    setxPos(xPos);
    setyPos(yPos);
    batch = new SpriteBatch();
    time = 0;
    loadAnimation();
    }
    
    
    
    
     public void loadAnimation() {
        // TODO Auto-generated method stub
        //batch = new SpriteBatch();
        animation = new Animation(1 / 3f, 
                new TextureRegion(new Texture("coin1.png")), 
                new TextureRegion(new Texture("coin2.png")),
                new TextureRegion(new Texture("coin3.png")), 
                new TextureRegion(new Texture("coin4.png")), 
                new TextureRegion(new Texture("coin5.png")), 
                new TextureRegion(new Texture("coin6.png")), 
                new TextureRegion(new Texture("coin7.png")), 
                new TextureRegion(new Texture("coin8.png")));
        animation.setPlayMode(Animation.PlayMode.LOOP);
    }