我创建了,正如您在下面看到的动画,但现在,我无法弄清楚如何添加到舞台。谁能告诉我怎么样?我在网上搜索并且不清楚它的想法。谢谢
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 :(得分:3)
就像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);
答案 1 :(得分:1)
最好的想法是创建一个扩展Actor类的actor,它将包装Animation对象。然后在他的 act mathod中,您获得了当前的KeyFrame,并且在 draw 方法中,您可以根据演员的位置呈现它
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());
}
}
现在您可以创建actor并将其添加到舞台上。
这种方法更好,因为:
答案 2 :(得分: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();
}