title: how can i start an animation on touch which will stop after reaching the end of spritesheet?
body: <p>I wanted to create a kicking animation when user touch the screen. I call a function from <code>render(){}</code> like this</p>
  
  <pre><code>if(Gdx.input.isTouched()) {
           if(lr.contains(Gdx.input.getX(), Gdx.input.getY())){
               //start animation 1-2-3-3-2-1 complete kick
               myAnimation(); //this is my function. code below,
               batch.begin();
               sprite.draw(batch);
               batch.end();
           }
  }
  </code></pre>
  
  <p><code>myAnimation()</code> is where i create a new task. to understand every movement i kept interval 5sec.</p>
  
  <p><strong>myAnimation()</strong></p>
  
  <pre><code>private void myAnimation(){
      Timer.schedule(new Task() {
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              if(positiveCycle){
                  currentFrame++;
                  if(currentFrame&gt;3) {
                      positiveCycle = false; 
                      currentFrame = 3;
                  }
              }else{
                  currentFrame--;
                  if(currentFrame==0){
                      //get out of this loop
                      currentFrame = 1;
                      positiveCycle = true;
                      this.cancel();
                  }
              }
              currentAtlasKey = String.format("%04d", currentFrame);
              sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
          }
      }
  }
  </code></pre>
  
  <p>But when I touch the screen i do not see each spite after 5 sec interval, Its kind of very quick to show the sprite. like less then 20 milliseconds.</p>
  
  <p><strong>The complete code</strong></p>
  
  <pre><code>package com.geek.tanvir.KickingGame;
  import com.badlogic.gdx.ApplicationListener;
  import com.badlogic.gdx.Gdx;
  import com.badlogic.gdx.graphics.GL10;
  //import com.badlogic.gdx.graphics.GLTexture;
  import com.badlogic.gdx.graphics.OrthographicCamera;
  import com.badlogic.gdx.graphics.Texture;
  import com.badlogic.gdx.graphics.g2d.Sprite;
  //import com.badlogic.gdx.graphics.Texture.TextureFilter;
  //import com.badlogic.gdx.graphics.g2d.Sprite;
  import com.badlogic.gdx.graphics.g2d.SpriteBatch;
  import com.badlogic.gdx.graphics.g2d.TextureAtlas;
  import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
  //import com.badlogic.gdx.graphics.g2d.TextureRegion;
  import com.badlogic.gdx.math.Rectangle;
  import com.badlogic.gdx.utils.Timer;
  import com.badlogic.gdx.utils.Timer.Task;
  
  public class KickingGame implements ApplicationListener {
  private OrthographicCamera camera;
  
  private SpriteBatch batch;
  private Texture texturePlayer;
  private Texture texturePlayerKick;
  private Texture texturePlayerKick2;
  private Texture texturePlayer2;
  private Texture ball;
  private TextureAtlas textureAtlas;
  private Sprite sprite;
  private int currentFrame = 1;
  private String currentAtlasKey = new String("0001");
  int currentframe = 1;
  private boolean positiveCycle = true;
  //private Sprite sprite;
  private Rectangle lr;
  private Rectangle rr;
  
  @Override
  public void create() {      
      //float w = Gdx.graphics.getWidth();
      //float h = Gdx.graphics.getHeight();
  
      camera = new OrthographicCamera();
      camera.setToOrtho(false, 800, 480);
      batch = new SpriteBatch();
  
      Texture.setEnforcePotImages(false);
  
      texturePlayer = new Texture(Gdx.files.internal("player.jpg"));
      texturePlayerKick = new Texture(Gdx.files.internal("playerKick.jpg"));
      texturePlayer2 = new Texture(Gdx.files.internal("player2.jpg"));
      texturePlayerKick2 = new Texture(Gdx.files.internal("playerKick2.jpg"));
      ball = new Texture(Gdx.files.internal("ball.jpg"));
  
      lr = new Rectangle();
      rr = new Rectangle();
  
      textureAtlas = new TextureAtlas(Gdx.files.internal("sheet.atlas"));
      AtlasRegion region = textureAtlas.findRegion("0001");
      sprite = new Sprite(region);
      sprite.setPosition(120, 100);
      sprite.scale(0.02f);
  
      lr.set(0, 0, 400, 400);
      rr.set(400, 0, 400, 400);
  }
  
  @Override
  public void dispose() {
      batch.dispose();
      texturePlayer.dispose();
      texturePlayer2.dispose();
      texturePlayerKick.dispose();
      texturePlayerKick2.dispose();
      ball.dispose();
  }
  
  private void myAnimation(){
      Timer.schedule(new Task() {
  
          @Override
          public void run() {
              // TODO Auto-generated method stub
              if(positiveCycle){
                  currentFrame++;
                  if(currentFrame&gt;3) {
                      positiveCycle = false; 
                      currentFrame = 3;
                  }
              }else{
                  currentFrame--;
                  if(currentFrame==0){
                      //get out of this loop
                      currentFrame = 1;
                      positiveCycle = true;
                      this.cancel();
                  }
              }
              currentAtlasKey = String.format("%04d", currentFrame);
              sprite.setRegion(textureAtlas.findRegion(currentAtlasKey));
          }
      }, 5, 5);
  }
  
  @Override
  public void render() {      
      Gdx.gl.glClearColor(1, 1, 1, 1);
      Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  
      camera.update();
      batch.setProjectionMatrix(camera.combined);
  
      batch.begin();
      batch.draw(texturePlayer, 300-texturePlayer.getWidth(),100);
      batch.draw(texturePlayer2, 500, 100);
      batch.draw(ball, 400-ball.getWidth(),500-ball.getHeight());
  
      batch.end();
      if(Gdx.input.isTouched()) {
           if(lr.contains(Gdx.input.getX(), Gdx.input.getY())){
               //start animation 1-2-3-3-2-1 complete kick
               myAnimation();
               batch.begin();
               sprite.draw(batch);
               batch.end();
           }
      }
  }
  
  @Override
  public void resize(int width, int height) {
  }
  
  @Override
  public void pause() {
  }
  
  @Override
  public void resume() {
  }
  }
  </code></pre>