Java 动画添加到舞台上
我创建了一个动画,你可以在下面看到,但现在,我不知道如何添加到舞台上。谁能告诉我怎么做?我在网上搜索,对它的想法不清楚。多谢各位Java 动画添加到舞台上,java,android,animation,libgdx,box2d,Java,Android,Animation,Libgdx,Box2d,我创建了一个动画,你可以在下面看到,但现在,我不知道如何添加到舞台上。谁能告诉我怎么做?我在网上搜索,对它的想法不清楚。多谢各位 TextureRegion tex1 = new TextureRegion(new Texture("play_anim_1")); TextureRegion tex2 = new TextureRegion(new Texture("play_anim_2")); TextureRegion tex3 = new TextureRegion(new Textur
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 ) ;
?溶液
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();
}
解决方案
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();
}
最好的方法是创建一个actor扩展类,该类将包装动画对象。然后在他的演技方法中,您将获得当前关键帧,在绘制方法中,您可以根据演员的位置进行渲染
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索引将始终保持-在您的示例中,动画将始终覆盖所有内容,因为它是在后台渲染的
- 您可以在单个类中封装更多代码,甚至可以继承它创建下一个动画类型或将动画与身体连接等
- 您不需要在渲染屏幕的方法中处理渲染
- Z索引将始终保持-在您的示例中,动画将始终覆盖所有内容,因为它是在后台渲染的
- 您可以在单个类中封装更多代码,甚至可以继承它创建下一个动画类型或将动画与身体连接等
- 最好的办法是创建一个actor扩展类,该类将包装动画对象。然后在他的演技方法中,您将获得当前关键帧,在绘制方法中,您可以根据演员的位置进行渲染
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());
}
}
现在您可以创建演员并将其添加到舞台上
这种方法更好,因为:
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);
就像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);
我可以试试你的想法,你可以用动画完整地写吗?我可以试试你的想法,你可以用动画完整地写吗?