Java 如何启动触控动画,并在到达spritesheet末尾后停止?

Java 如何启动触控动画,并在到达spritesheet末尾后停止?,java,android,libgdx,Java,Android,Libgdx,我想在用户触摸屏幕时创建一个踢腿动画。我像这样从render(){}调用函数 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,

我想在用户触摸屏幕时创建一个踢腿动画。我像这样从
render(){}
调用函数

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();
         }
}
myAnimation()
是我创建新任务的地方。为了理解每一个动作,我保持5秒的间隔

myAnimation()

但是当我触摸屏幕的时候,我看不到每一个画面,间隔5秒后,它会很快显示精灵。大概不到20毫秒

完整的代码

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>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() {
}
}

我想您想显示一个
精灵
,它一直站着,直到用户触摸为止。然后它制作
动画
,如果完成,它会再次静止,直到用户再次触摸,对吗? 为此,请使用并使用其第二个构造函数:

Animation myAnim = new Animation(frameDuration, animationTextures, PlayMode.Normal);
然后存储一个
布尔动画=false以及
浮点状态时间=0

如果用户触摸您,则设置
动画=真

在渲染中,请说:

if (animating) {
    stateTime+=delta;
    TextureRegion currentFrame = myAnim.getKeyFrame(stateTime);
    // draw the currentFrame
    if(myAnim. isAnimationFinished(stateTime)) {
        animating = false;
        stateTime = 0;
    }
}
else {
   // draw the unanimated Frame;
}
要读的东西:

欢迎概念性回答。动画将在完成一个完整的循环后结束,如1-2-3-3-2-1(精灵表中的精灵)。您是否试图制作一个精灵仅在用户仍在触摸时显示的动画?否,这是一个错误。sprite.draw()应在if条件之外。它试过了,但我没有得到想要的结果。谢谢。我几乎是用同样的方法做的。现在我从你的ans知道了一些额外的东西。再次感谢。没问题。所以我是来帮忙的。如果你还有任何问题,请随时提问
if (animating) {
    stateTime+=delta;
    TextureRegion currentFrame = myAnim.getKeyFrame(stateTime);
    // draw the currentFrame
    if(myAnim. isAnimationFinished(stateTime)) {
        animating = false;
        stateTime = 0;
    }
}
else {
   // draw the unanimated Frame;
}