2015-10-20 90 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);