# Animations not working using scene2d and actor class in LibGDX

I have a problem making a simple animation using the Actor class.

public class fondoanima extends Actor {

protected TextureRegion currentFrame;
protected TextureRegion[] walkFrames;
protected Animation walkAnimation, idleAnimation, currentAnimation;
private Texture texture;
float stateTime;

public fondoanima() {

texture = new Texture(Gdx.files.internal("image.png"));
TextureRegion[][] tmp = TextureRegion.split(texture, texture.getWidth()/5, texture.getHeight() / 1);

walkFrames = new TextureRegion[5];
for (int j = 0; j < 5; j++) {
walkFrames[j] = tmp[0][j];
}

walkAnimation = new Animation(0.4f, walkFrames);
currentAnimation = walkAnimation;

stateTime = 0f;

}

@Override
public void draw(Batch batch, float parentAlpha) {
// TODO Auto-generated method stub
super.draw(batch, parentAlpha);

Color col = getColor();
batch.setColor(col.r, col.g, col.b, col.a*parentAlpha);
batch.draw(currentFrame, 10, 10);

}

@Override
public void act(float delta) {
// TODO Auto-generated method stub
super.act(delta);

stateTime += Gdx.graphics.getDeltaTime();
currentFrame = currentAnimation.getKeyFrame(stateTime, true);
}

public void dispose(){}


I have been guided by different examples and the wiki, But I get not make it work. It is an animation that will be as the background. (image.png=>1600x350=>5 images are linked horizontally). Thanks.

Solution: initialize currentFrame = tmp[0][0];

• At first glance the code looks ok other than you can use the delta passed into act instead of getting it again with getDeltaTime(). What results are you getting? Is it drawing anything at all? – spectacularbob Aug 8 '16 at 16:58
• Hi, in the game, I 'm adding actors on stage and there is no problem, when I add this actor, the game freezes and I get an error "RuntimeException". – Txoloentertainment Aug 10 '16 at 8:23
• I tried what you tell me but it does not work. the change you propose, is this? stateTime += delta; Thank you very much. – Txoloentertainment Aug 10 '16 at 8:30
• yes, use stateTime += delta; although this is not a fix, it is just an optimization because delta and Gdx.graphics.getDeltaTime() will be the same value. What is the message from the RuntimeException? It will probably lead you to your answer. – spectacularbob Aug 10 '16 at 16:05
• The message: "Thread [lwjgl application] (suspended (exception nullpointerexception))" I checked the declarations and initializations, and I can not find the problem. Thanks for answering spectacularbob. – Txoloentertainment Aug 11 '16 at 9:41

Solution: initialize currentFrame = tmp[0][0];