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;   


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


public void act(float delta) {
    // TODO Auto-generated method stub

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

  • \$\begingroup\$ 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? \$\endgroup\$ – spectacularbob Aug 8 '16 at 16:58
  • \$\begingroup\$ 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". \$\endgroup\$ – Txoloentertainment Aug 10 '16 at 8:23
  • \$\begingroup\$ I tried what you tell me but it does not work. the change you propose, is this? stateTime += delta; Thank you very much. \$\endgroup\$ – Txoloentertainment Aug 10 '16 at 8:30
  • \$\begingroup\$ 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. \$\endgroup\$ – spectacularbob Aug 10 '16 at 16:05
  • \$\begingroup\$ 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. \$\endgroup\$ – Txoloentertainment Aug 11 '16 at 9:41

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

  • \$\begingroup\$ Why is that the solution? \$\endgroup\$ – MichaelHouse Aug 13 '16 at 16:14
  • \$\begingroup\$ "The NullPointerException occurs when an object is used for some action when the object itself is null because it likely hasn't been initialized." so the solution is initialize currentFrame. \$\endgroup\$ – Txoloentertainment Aug 16 '16 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.