簡體   English   中英

在Java(Android)中libgdx只需觸摸即可繪制/渲染動畫

[英]In Java (Android) libgdx draw/ render animation when just touched

在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));
    }

第二個參數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));
     }

@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));
       }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM