0
\$\begingroup\$

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

\$\endgroup\$
10
  • \$\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\$ Aug 8, 2016 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\$ Aug 10, 2016 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\$ Aug 10, 2016 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\$ Aug 10, 2016 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\$ Aug 11, 2016 at 9:41

1 Answer 1

-1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ Why is that the solution? \$\endgroup\$
    – House
    Aug 13, 2016 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\$ Aug 16, 2016 at 8:28

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .