2017-03-08 107 views
0

我認爲在我的動畫中加載很多紋理會導致我的遊戲略微滯後,並且我希望我的數組紋理在TextureAtlas中。下面的代碼是我的動畫代碼,它也將獲得動畫中的紋理幀索引,並在顯示紋理索引時執行任務....我已經爲紋理圖集命名了一個名爲shot.pack的.pack文件,但是我不知道如何應用它並使其成爲這樣。我希望有人能夠幫助紋理紋理圖集和動畫

 Texture[] shot; 
    //At CONSTRUCTOR 
    shot = new Texture[21]; 
    for(int i=0; i <21; i++){ 
     if(i == 19 || i==20){ 
      shot[i]=new Texture("s18.png"); 
     } 
     else { 
      shot[i] = new Texture("s" + Integer.toString(i) + ".png"); 
     } 

    } 
     //animation 
    animation = new Animation(1/19f,shot); 


    //@render METHOD 
    sb.draw((Texture) animation.getKeyFrame(timePassed, false), ShootingTreys.WIDTH * 0.03f, ShootingTreys.HEIGHT * 0.03f, (ShootingTreys.WIDTH * 1/6), (ShootingTreys.HEIGHT/2) + (ShootingTreys.HEIGHT * 0.13f)); 

     if (animation.getKeyFrameIndex(timePassed) == 20) { 
      isShotDelay = true; 
      shotDelay += Gdx.graphics.getDeltaTime(); 

      if (shotDelay >= 0.2f || ball.getBall().getY() < ShootingTreys.HEIGHT * 0.2f) { 
       touch = false; 
       timePassed = 0; 
       shotDelay = 0; 
       isShotDelay = true; 
       //for missed ball 
       colide = false; 
      } 
     } 

回答

0

保持動畫的所有幀在一個單獨的文件.pack

TextureAtlas aniAtlas=new TextureAtlas("ani.pack"); 
    Array<TextureAtlas.AtlasRegion> animationFrame=aniAtlas.getRegions(); 
    float duration=.4f; 
    Animation<TextureRegion> animation=new Animation<TextureRegion>(duration,animationFrame); 

多個動畫的文件中的一個.pack 保持索引概念相似類型例如像文件。
bird_0.png,bird_1.png,.....
cat_0.png,cat_1.png,.....

TextureAtlas multiAniAtlas=new TextureAtlas("ani.pack"); 
    Array<TextureAtlas.AtlasRegion> birdFrames=multiAniAtlas.findRegions("bird"); 
    float duration1=.55f; 
    Animation<TextureRegion> birdAnimation=new Animation<TextureRegion>(duration1,birdFrames); 

使用可以使用AssetManger並加載您TextureAtlas這樣:

AssetManager assetManager=new AssetManager(); 
    assetManager.setLoader(TextureAtlas.class, new TextureAtlasLoader(new InternalFileHandleResolver())); 
    assetManager.load("ani.pack", TextureAtlas.class); 
    assetManager.finishLoading(); 

    TextureAtlas atlas=assetManager.get("ani.pack"); 
+0

如果動畫完成,我該如何顯示一個幀? –

+0

我在紋理打包器中的紋理沒有按順序 –

+0

爲動畫渲染放置了一個條件,並在該條件下顯示單幀。 – Aryan