1

I was developing a game using the Libgdx game library. I came up with this problem while trying to animate from one corner point to touch point using libgdx. Used this tutorial for understanding basics of Libgdx animation basics Link.

I am not able to find how animations in libgdx works,Normal movement of frames takes place but how to start single time normal animation upon touch on screen. Thanks in advance for help.

Edit: My class implements Screen This is what i tried

In class default constructor

animation=new Animation(1/15f, atlas1.getRegions());

In Render method :- To check touch

public void touched()
{
    if(Gdx.input.isTouched())
    {
        touched = true;
        x = Gdx.input.getX();
        y = Gdx.input.getY();
        velocityX = (x - animationX) / 100;
        velocityY = (y - animationY) / 100;
    }
}

After calling touch method For animation

public void anim()
{
    if (touched) 
    {
         elapsedTime += Gdx.graphics.getDeltaTime();
         //animationX += velocityX;
        // animationY += velocityY;
         batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
         animfinished=animation.isAnimationFinished(elapsedTime);
     }
    if(touched)
    {
          batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
    }
}
5
  • You need to specify one thing, are you using Scene2D or not? Animation on actors and animation on your own objects is fairly different. Jul 16, 2014 at 8:22
  • I am animating different objects which i have stored in pack/png.
    – ManishSB
    Jul 16, 2014 at 8:24
  • but are you using SpriteBatch or Scene2d and Actors Jul 16, 2014 at 8:26
  • i am using SpriteBatch @Zhuinden
    – ManishSB
    Jul 16, 2014 at 8:28
  • @Zhuinden i have put whatever i tried till now,but the animation is repeating if i put looping true and if i put it to false it repeats at only 1 position.
    – ManishSB
    Jul 17, 2014 at 7:17

2 Answers 2

3

You can use InputListener for touch event and use this to control your animation.

Below is simple approach. Of course, more proper would be to extend Animation class and move there all animation logic.

public class AnimationTest implements ApplicationListener, InputListener {

    boolean touched = false;
    Animation animation;
    float elapsedTime = 0;
    float touchedX, touchedY;
    float animationX, animationY;
    float velocityX, velocityY;

    //  ... do not forget to register InputListener here ...

    @Override
    public boolean touchDown(InputEvent event, float x, float y, int pointer,int button) {
        touched = true;
        touchedX = x;
        touchedY = y;
        velocityX = (touchedX - animationX) / 100;
        velocityY = (touchedY - animationY) / 100;
    }

    // ... 
    
    @Override
    public void render() {
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

        batch.begin();

        if (touched) {
            elapsedTime += Gdx.graphics.getDeltaTime();
            animationX += velocityX;
            animationY += velocityY;
            batch.draw(animation.getKeyFrame(elapsedTime, true), animationX, animationY);
        }
        
        batch.end();
    }

}

This is where animation occurs:

batch.draw(animation.getKeyFrame(elapsedTime, true), animationX , animationY);

You just gets appropiate keyframe from your animation and draws this on batch.

0
0
public void anim()
{
    if (touched) 
    {
         elapsedTime += Gdx.graphics.getDeltaTime();
         //animationX += velocityX;
        // animationY += velocityY;
         batch.draw(animation.getKeyFrame(elapsedTime, false), x, y);
         animfinished=animation.isAnimationFinished(elapsedTime);
     }
    if(touched)
    {
          batch.draw(bullettouch, x, y, bullettouch.getRegionWidth(), bullettouch.getRegionHeight());
    }
}
1
  • 2
    A few words of explanation would also be helpful. Aug 27, 2014 at 11:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.