2014-07-13 139 views
0

試圖有一個旋轉和動畫的2D,自上而下的精靈,我有它被渲染的主類,和一個單獨的'資產'類創建它。旋轉工作,但精靈停留在同一幀。我用libgdx維基嘗試和動畫我的精靈https://github.com/libgdx/libgdx/wiki/2D-Animation雪碧動畫Libgdx爪哇

這裏是資產類的代碼:

public class Asset implements ApplicationListener, Screen{ 

    public static Texture walkSheet; 




    private static final int FRAME_COLS = 4;  
    private static final int FRAME_ROWS = 2;  

    static Animation   walkAnimation;  
    static TextureRegion[]   walkFrames;  
    static TextureRegion   currentFrame;  
    static SpriteBatch spriteBatch; 

    static float stateTime;     


    public static void load(){ 
     walkSheet = new Texture(Gdx.files.internal(".png")); 


     TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth()/FRAME_COLS, walkSheet.getHeight()/FRAME_ROWS);    // #10 
     walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS]; 
     int index = 0; 
     for (int i = 0; i < FRAME_ROWS; i++) { 
      for (int j = 0; j < FRAME_COLS; j++) { 
       walkFrames[index++] = tmp[i][j]; 
      } 
     } 
     walkAnimation = new Animation(0.1f, walkFrames);  // #11 
     spriteBatch = new SpriteBatch();    // #12 
     stateTime = 0f; 
     stateTime += Gdx.graphics.getDeltaTime(); 
     currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    } 

這裏是它的呈現方式:

game.batch.draw(Asset.currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270); 

我該怎麼需要做正確的動畫精靈?

回答

1

該框架永遠不會動畫,因爲您沒有更新它。在load()方法你做了以下內容:

stateTime += Gdx.graphics.getDeltaTime(); 
currentFrame = walkAnimation.getKeyFrame(stateTime, true); 

但是當你調用load()方法會執行這個操作一次。

當你的Asset類實現Screen接口,你必須實現的抽象方法​​,所以你需要在該方法來更新框架如下:從

public void render(float delta) 
{ 
    stateTime += delta; 
    currentFrame = walkAnimation.getKeyFrame(stateTime, true); 
    // Then render the frame as follows 
    batch.draw(currentFrame, x, y, (float)85, (float)85, (float)170, (float)170, (float)1, (float)1, (float)angleDegrees + 270); 
} 
0
// pass the array of movements to AnimatedActor 
// animationFrames = walkSheetArray[moveDirection]; 
// animation = new Animation(1f/5f, animationFrames); 
// myAnimatedActor = new AnimatedActor(animation, rotation) 

public class AnimatedActor extends Image { 
private float stateTime = 0; 
Animation animation; 
public AnimatedActor(Animation animation, float rotation) { 
super(animation.getKeyFrame(0)); 
this.animation = animation; 
this.rotation = rotation; 
} 
@Override 
public void act(float delta) { 
((TextureRegionDrawable) getDrawable()).setRegion(animation.getKeyFrame(stateTime += delta, true)); 
myAnimatedActor.setRotation(rotation); 
super.act(delta); 
    } 
} 

walk frames 8x8

+0

圖像http://www.reinerstilesets.de/ –