在Java(Android)中libgdx只需触摸即可绘制/渲染动画

时间:2017-02-22 06:35:07

标签: java android libgdx

在Java(Android)中,libgdx在触摸时绘制/渲染动画,否则它将显示动画中的特定帧。问题是,当我触摸屏幕时,它只会将动画渲染一秒钟。

   //@render   Spritebatch sb;

   if(Gdx.input.justTouched()){
        sb.draw((Texture) animation.getKeyFrame(timePassed,true), Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
    }

    else{
        sb.draw(shot[1], Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
    }

2 个答案:

答案 0 :(得分:0)

第二个参数getKeyFrame(..)启用循环动画​​是否循环。设置为true。为什么不使用animation.getKeyFrame(stateTime)代替animation.getKeyFrame (float stateTime, boolean looping)。确保循环Animation.PlayMode

 if(!Gdx.input.justTouched()){
         sb.draw(shot[1], Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
 }
 else{
       sb.draw((Texture) animation.getKeyFrame(timePassed,true), Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
     }

答案 1 :(得分:0)

@AbhishekAryan谢谢先生,我得到了答案

 timePassed += Gdx.graphics.getDeltaTime();
    if(Gdx.input.justTouched() || touch==true){
        touch=true;
        sb.draw((Texture) animation.getKeyFrame(timePassed,true), Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
            if(animation.getKeyFrameIndex(timePassed) == 18){
                touch=false;
                timePassed=0;
            }
    }
    else if(touch == false){
        sb.draw(shot[1], Gdx.graphics.getWidth() * 0.03f, Gdx.graphics.getHeight() * 0.03f, (Gdx.graphics.getWidth() * 1 / 6), (Gdx.graphics.getHeight() / 2) + (Gdx.graphics.getHeight() * 0.13f));
       }