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.