1
\$\begingroup\$

In my game I have some box2d bodies where I add sprites using the following code in my render() method.

for (Body body : worldBodies) {
        if (body.getUserData() instanceof Sprite) {
            Sprite sprite = (Sprite) body.getUserData();

            Vector2 position = body.getPosition();
            sprite.setPosition(position.x - sprite.getWidth() / 2 , position.y - sprite.getHeight() / 2);
            sprite.setRotation(body.getAngle() * MathUtils.radiansToDegrees);
            sprite.draw(batch);
        }
    }

One of the bodies has to be animated.

birdAnimation = new Animation(1, birdAtlas.getRegions());
birdAnimation.setPlayMode(Animation.PlayMode.LOOP_PINGPONG);

This is the animation and now I tried to set the body's sprite obstacle6 to the current textureRegion from the Animation unsing this code:

obstacle6.setRegion(birdAnimation.getKeyFrame(delta));

Somehow it just shows the first texture of the atlas. How can I get it to change? Or is there an other way to animate a box2d body? If you need any other information just comment.

\$\endgroup\$
1
\$\begingroup\$

Don't pass delta to getKeyFrame, getKeyFrame takes the state time. The state time is likely something like the sum of all the deltas you've seen so far.

The state time indicates how "far" into the animation you've gone, and by constantly passing delta (which if you're running at 60 fps will be 0.016s) the Animation will always yield the first frame.

\$\endgroup\$

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.