I have a class that takes a TextureRegion+number of columns and number of rows of this region and builds an Animation object. The problem is with the second consructor. I’m trying to build an animation of a walking-spider with one row and ten columns.
public class AnimationRegions extends Actor {
private Animation walkAnimation;
private TextureRegion walkSheet;
private TextureRegion[] walkFrames;
private Sprite currentFrame;
private float elapsedTime = 0f;
public AnimationRegions(TextureRegion sheet, int Columns, int Rows,
float duration) {
walkSheet = sheet;
TextureRegion[][] tmp = walkSheet.split(walkSheet.getRegionWidth()
/ Columns, walkSheet.getRegionHeight() / Rows);
walkFrames = new TextureRegion[Columns * Rows];
int index = 0;
for (int i = 0; i < Rows; i++) {
for (int j = 0; j < Columns; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(duration, walkFrames);
walkAnimation.setPlayMode(PlayMode.LOOP_REVERSED);
currentFrame = new Sprite();
}
public AnimationRegions(TextureRegion sheet, int Columns, int Rows,
float duration, int startingRow) {
walkSheet = sheet;
TextureRegion[][] tmp = walkSheet.split(walkSheet.getRegionWidth()
/ Columns, walkSheet.getRegionHeight() / Rows);
walkFrames = new TextureRegion[Columns * Rows];
int index = 0;
for (int i = startingRow; i < Rows; i++) {
for (int j = 0; j < Columns; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(duration, walkFrames);
walkAnimation.setPlayMode(PlayMode.LOOP_REVERSED);
currentFrame = new Sprite();
}
@Override
public void act(float delta) {
super.act(delta);
elapsedTime += delta;
currentFrame.setRegion(walkAnimation.getKeyFrame(elapsedTime, true));
currentFrame.setSize(getWidth(), getHeight());
currentFrame.setPosition(getX(), getY());
currentFrame.setOrigin(currentFrame.getX() - currentFrame.getWidth()
/ 2, currentFrame.getY() - currentFrame.getHeight() / 2);
}
@Override
public void draw(Batch batch, float parentAlpha) {
super.draw(batch, parentAlpha);
currentFrame.draw(batch);
}
@Override
public void setScale(float scaleXY) {
super.setScale(scaleXY);
currentFrame.setScale(scaleXY);
}
@Override
public float getScaleX() {
return currentFrame.getScaleX();
}
@Override
public float getScaleY() {
return currentFrame.getScaleY();
}
public Sprite getCurrentFrame() {
return currentFrame;
}
public boolean isAnimationFinished() {
return walkAnimation.isAnimationFinished(elapsedTime);
}
public TextureRegion getRegion() {
return currentFrame;
}
public void resetTime() {
elapsedTime = 0;
}
The problem is with this line:
currentFrame.setRegion(walkAnimation.getKeyFrame(elapsedTime, true));
I ran some tests and apperantely this call:
walkAnimation.getKeyFrame(elapsedTime, true)
returns null. I have no idea why.
Here is the full error:
Exception in thread "LWJGL Application" java.lang.NullPointerException
at com.badlogic.gdx.graphics.g2d.TextureRegion.setRegion(TextureRegion.java:112)
at engine.utils.graphics.AnimationRegions.act(AnimationRegions.java:103)
at com.badlogic.gdx.scenes.scene2d.Group.act(Group.java:50)
at com.badlogic.gdx.scenes.scene2d.Group.act(Group.java:50)
at com.badlogic.gdx.scenes.scene2d.Group.act(Group.java:50)
at com.badlogic.gdx.scenes.scene2d.Stage.act(Stage.java:223)
at views.screens.GameScreen.render(GameScreen.java:74)
at com.badlogic.gdx.Game.render(Game.java:46)
at engine.MonkeyRun.render(MonkeyRun.java:68)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:207)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:114)