2
\$\begingroup\$

I'm trying move animation within screen, here's the code

Animation<TextureRegion> animation_run = GifDecoder.loadGIFAnimation(Animation.PlayMode.LOOP, 
    Gdx.files.internal("run.gif").read());
    float elapsedTime += (Gdx.graphics.getDeltaTime()) * 2;
    float playablePosition = (Gdx.graphics.getWidth()) / 2 - 200;
    float currentPosition = 0;
    for (int i = 0; currentPosition <= playablePosition; i++) {
    currentPosition += i;
    batch.begin();
    batch.draw(animation_run.getKeyFrame(elapsedTime), currentPosition, 0, 200, 300);
batch.end();
}

but I got not exactly what I wantenter image description here

Does anybody know how fix my code to make movement from left edge to the center properly?

\$\endgroup\$

1 Answer 1

1
\$\begingroup\$

Your problem lies in this code

for (int i = 0; currentPosition <= playablePosition; i++) {
    currentPosition += i;
    batch.begin();
    batch.draw(animation_run.getKeyFrame(elapsedTime), currentPosition, 0, 200, 300);
    batch.end();
}

Instead of looping through adding the animation we want to just draw the animation once in the batch and draw it in different position each frame. Below I change the position of the animation by incrementing position

Animation<TextureRegion> animation_run = GifDecoder.loadGIFAnimation(Animation.PlayMode.LOOP, 
Gdx.files.internal("run.gif").read());
float elapsedTime += (Gdx.graphics.getDeltaTime()) * 2;
float playablePosition = (Gdx.graphics.getWidth()) / 2 - 200;
// float currentPosition = 0; // <-- this needs set outside ouside of the render method.
currentPosition += 1;
batch.begin();
batch.draw(animation_run.getKeyFrame(elapsedTime), currentPosition, 0, 200, 300);
batch.end();
\$\endgroup\$

You must log in to answer this question.

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