Search code examples
libgdx

animation does not render properly


I am rendering a sprite created in another class with this code:

    batch = new SpriteBatch();
    textureAtlas = new TextureAtlas(Gdx.files.internal("bird.atlas"));
    animation = new Animation(1/6f, textureAtlas.getRegions());

My problem is that it does not run the different images, instead i get a static one. If i change the speed in animation to 1/10000f it will flip thrue the images like crasy.

This is the code in render method in the bird class, then i run renderer.render(); from the game class.

    batch.begin();
    elapsedTime = Gdx.graphics.getDeltaTime();
    batch.draw(animation.getKeyFrame(elapsedTime, true), 50, 50);
    batch.end();

this will show my bird ontop of my tiled map but then the bird does not flaps his wings :( very sad.

libgdx version: 1.5.5

Any ideas as where i am going astray?


Solution

  • An animation needs elpased time, not delta time. Your elapsedTime variable is only storing delta time.

    Change elapsedTime = Gdx.graphics.getDeltaTime(); to elapsedTime += Gdx.graphics.getDeltaTime();.