sync of body and sprites for animation?

23 Views Asked by At

First, made an Animation object for rendering character moving. And, I want to make body for animation. In this case, how can I change actual fixture for animation frames? Or, should I use box2d joints instead of drawing animation frames?

Here is code for making body:


public class MyGame extends Game {
    physicsBodies = new PhysicsShapeCache("physics.xml");
    // ...
    public Body createBody(String name, float x, float y, float rotation) {
        Body body = physicsBodies.createBody(name,  world,  SCALE,  SCALE);
        body.setTransform(x,  y, rotation);
        return body;
    }
}

public class Player extends Actor {
    public Player(float w, float h, RunGame g) {
        body = game.createBody("running_0", getX(), getY(), 0);
    }
}

Here is my code for animation:


public class Assets {
    static TextureAtlas texAtlas;
    
    static {
        texAtlas = new TextureAtlas("spritesheet.txt");
    }

}
// ...
public class Player extends Actor {
    Animation<Sprite> runningAnimation;
    float elapsedTime;

    runningAnimation=new Animation<>(0.1f,Assets.texAtlas.createSprites("running"),Animation.PlayMode.LOOP);

    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);

        if (isMoving()) {
            elapsedTime += Gdx.graphics.getDeltaTime();
            currentFrame++;
        }

        Sprite sprite = runningAnimation.getKeyFrame(elapsedTime, true);
        sprite.setX(getX());
        sprite.setY(getY());

        sprite.draw(batch);
    }
}
0

There are 0 best solutions below