2015-10-20 145 views
0

我創建了一個動畫,但是現在我無法弄清楚如何添加到舞臺。誰能告訴我如何?我在網上搜索和不明確的想法。謝謝動畫添加到舞臺

TextureRegion tex1 = new TextureRegion(new Texture("play_anim_1")); 
TextureRegion tex2 = new TextureRegion(new Texture("play_anim_2")); 
TextureRegion tex3 = new TextureRegion(new Texture("play_anim_3")); 
TextureRegion tex4 = new TextureRegion(new Texture("play_anim_4")); 

Animation playerAnimation = new Animation(0.1f, tex1, tex2, tex3, tex4); 

,你可以這樣做

stage.addAnimation (playerAnimation) ; 

回答

0

解決方案

public void render() { 
    //qui definisco lo stage 
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT); 
    stage.act(Gdx.graphics.getDeltaTime()); 
    stage.draw(); 

    elapsedTime += Gdx.graphics.getDeltaTime(); 

    batch.begin(); 
    batch.draw(seaAnimation.getKeyFrame(elapsedTime,true),100,100); 
    batch.end(); 
} 
1

最好的辦法是創建擴展Actor類,將被包裹Animation對象的演員。然後,在他的行爲馬託你得到當前關鍵幀,並在方法,你可以根據演員的位置

class MyAnimation extends Actor 
    { 
     Animation animation; 
     TextureRegion currentRegion; 

     float time = 0f; 

     //... creating animation etc... 

     @Override 
     public void act(float delta){ 
      time += delta; 

      currentFrame = animation.getKeyFrame(time, true); 
     } 

     @Override 
     public void draw(Batch batch, float parentAlpha) { 
      super.draw(batch, parentAlpha); 
      batch.draw(currentRegion, getX(), getY()); 
     } 
    } 

現在可以創建演員,只是把它添加到舞臺上呈現它。


這種方法比較好,因爲:

  • 你並不需要處理渲染渲染畫面的方法
  • 的Z-索引將始終保持 - 在您的示例動畫將永遠在一切,因爲它是在階段
  • 之後呈現的,你可以在單個類中包裝更多的代碼,甚至可以繼承它創建下一個動畫類型或加入動畫與身體等...
+0

我可以試試你的想法,你甚至與動畫寫全? – Dev4Ever

1

就像m.antkowicz的代碼,創建類:

import com.badlogic.gdx.graphics.g2d.Animation; 
import com.badlogic.gdx.graphics.g2d.Batch; 
import com.badlogic.gdx.graphics.g2d.TextureRegion; 
import com.badlogic.gdx.scenes.scene2d.Actor; 

public class AnimeActor extends Actor{ 


    Animation animation; 
    TextureRegion currentRegion; 

    float time = 0f; 

    public AnimeActor(Animation animation) { 
     this.animation = animation; 
    } 

    @Override 
    public void act(float delta){ 
     super.act(delta); 
     time += delta; 

     currentRegion = animation.getKeyFrame(time, true); 
    } 

    @Override 
    public void draw(Batch batch, float parentAlpha) { 
     super.draw(batch, parentAlpha); 
     batch.draw(currentRegion, getX(), getY()); 
    } 
} 

使用:

AnimeActor anim = new AnimeActor(animation); 
stage.addActor(anim);