2017-08-03 71 views
0

My animation is not working 2D動畫,僅顯示靜態圖片。我根據libgdx/wiki的例子做了一切。爲什麼它不起作用?libgdx不工作

public class Thorns extends GameObject implements IGameObject { 

    Animation<TextureRegion> animation; 
    private static final int FRAME_COLS = 1, FRAME_ROWS = 2; 
    public Thorns(Texture texture, Body body) { 
     super(texture, body,false); 
     Texture walkSheet = new Texture("Thorns.png"); 
     TextureRegion[][] tmp = TextureRegion.split(walkSheet, 
      walkSheet.getWidth()/FRAME_COLS, 
      walkSheet.getHeight()/FRAME_ROWS); 

     TextureRegion[] walkFrames = new TextureRegion[FRAME_ROWS*FRAME_COLS]; 
     int index = 0; 
     for (int i = 0; i < FRAME_ROWS; i++) { 
      for (int j = 0; j < FRAME_COLS; j++) { 
       walkFrames[index++] = tmp[i][j]; 
      } 
     } 

     animation = new Animation<TextureRegion>(1.025f, walkFrames); 
    } 

    @Override 
    public void dispose() { 

    } 

    int stateTime; 

    @Override 
    public void draw(SpriteBatch spriteBatch) { 
     stateTime += Gdx.graphics.getDeltaTime(); 

     TextureRegion currentFrame = (TextureRegion) animation.getKeyFrame(stateTime, false); 

     spriteBatch.draw(currentFrame, body.getTransform().getPosition().x, body.getTransform().getPosition().y); 
     //drawSprite(spriteBatch); 

    } 
} 

動畫不啓動在所有

+0

是您Thorns.png圖像2個圖像並排?你是否在等待1.025秒以查看幀改變?它應該是非循環的,所以它只顯示1.025秒的第一個圖像? – dfour

+0

我的理解動畫根本不會啓動。一切都在第一幀。 環流式更改爲true,沒有任何改變。 – GRbI3yH

+0

我將幀更改時間增加了10.0f,但沒有像以前那樣發生 – GRbI3yH

回答

0

完全可能的動畫正在運行,但你沒有得到他的觀點,因爲你已經與幀持續時間1.025 sec兩種幀動畫。而你的動畫運行無環

嘗試環Aniamation以便傳遞true如getKeyFrame (float stateTime, boolean looping)方法的第二個參數。

@Override 
public void draw(SpriteBatch spriteBatch) { 
    stateTime += Gdx.graphics.getDeltaTime(); 
    TextureRegion currentFrame = animation.getKeyFrame(stateTime, true); 
    spriteBatch.draw(currentFrame, body.getTransform().getPosition().x, body.getTransform().getPosition().y); 
} 

EDIT

變更數據的stateTimefloatint類型。

+0

動畫根本無法啓動。一切都在第一幀。 循環更改爲true,沒有任何更改 – GRbI3yH

+0

@ GRbI3yH將'stateTime'的數據類型更改爲'float' – Aryan

+1

是的,它正在工作。謝謝您的幫助 – GRbI3yH