2014-03-14 54 views
1

如何使用單獨的精靈來實現libgdx動畫或textures.I嘗試作爲指定here在一個TexturePacker包裝圖像,也使一個PNG文件和映射文件來做到這一點,但它是因爲只有3張圖像需要動畫,所以效果不理想。請告訴我如何使用個人圖像或紋理進行動畫製作。動畫libgdx使用單獨的紋理

回答

1

好的首先,如果你有3張照片,你想創建一種精靈,他們確實需要相同的尺寸。我會把它作爲一種演員來實施。如果你不喜歡演員的想法,只需刪除擴展並添加位置和大小。還要將act更改爲update並刪除draw(...,...)的覆蓋。

public class IndividualSprite extends Actor { 

    private Array<Texture> textures; //meight change it to TextureRegions which also contains a texture but sizes and positions 
    private float intervaltime = 0; 
    private float interval; 
    private int count = 0; 

    /** 
    * Varargs<br> 
    * @param timeIntervall 
    * @param t 
    *   Varargs. Parse all textures here as list or simply with ,t1 
    *   ,t2 , t3 
    */ 
    public IndividualSprite(float timeIntervall, Texture... t) { 
     this.interval = timeIntervall; 
     this.textures = new Array<Texture>(); 
     for (Texture texture : t) { 
      this.textures.add(texture); 
     } 
     // now sett default width height and pos 
     this.setX(0); 
     this.setY(0); 
     this.setWidth(textures.get(0).getWidth()); 
     this.setWidth(textures.get(0).getHeight()); 
    } 

    public void addTexture(Texture... t) { 
     for (Texture texture : t) { 
      this.textures.add(texture); 
     } 
    } 

    public void deleteTexture(Texture... t) { 
     for (Texture texture : t) { 
      this.textures.removeValue(texture, true); 
     } 
    } 

    public void setInterval(float i) { 
     this.interval = i; 
    } 

    @Override 
    public void act(float delta) { 
     super.act(delta); 
     this.intervaltime += delta; 
     if (this.intervaltime >= interval) { 
      if (count == textures.size) { 
       count = 0; 
      } else { 
       count++; 
      } 
      this.intervaltime = 0; 
     } 
    } 

    @Override 
    public void draw(SpriteBatch batch, float alpha) { 
     batch.begin(); 
     batch.draw(textures.get(count), getX(), getY(), getWidth(), getHeight()); 
     batch.end(); 
    } 
} 

但是一個演員,你可以給他行動移動或任何。它是一種自定義的雪碧。這只是一種方法。應該有更多,甚至更好。您還可以擴展Sprite類並更改其它東西,以便使用多個Texture等。試試吧。這是演員解決方案或非演員解決方案,如果你改變我提到的事情。別忘了打電話給.act/update,否則它不會顯示第一個FirstTexture

+0

感謝您抽出interest.I另一個簡單的代碼做了。 –

0

找到了答案如下所述:

region1 = new TextureRegion(texture1); 
    region2 = new TextureRegion(texture2); 
    region3 = new TextureRegion(texture3); 

    animation = new Animation(0.08f, region1, region2, region3);