Java 如何在LibGDX的表格布局中添加动画?

Java 如何在LibGDX的表格布局中添加动画?,java,libgdx,Java,Libgdx,我有一个表,其中包含带有图像和文本的行。现在,我决定用旋转动画交换图像。是否有办法在表格中完成此操作,或者我必须使用SpriteBatch来完成此操作?如果是,是否有办法保留我的表并以某种方式找到图像所在行/列的正确位置? 我不愿意扔掉这张桌子,因为有很大一部分文字。使用setWrap标志可以更轻松地完成任务。您必须在构造函数的开头加载所有内容 private void loadTextures() { walkSheet = new Texture(Gdx.files.interna

我有一个表,其中包含带有图像和文本的行。现在,我决定用旋转动画交换图像。是否有办法在表格中完成此操作,或者我必须使用
SpriteBatch
来完成此操作?如果是,是否有办法保留我的表并以某种方式找到图像所在行/列的正确位置?
我不愿意扔掉这张桌子,因为有很大一部分文字。使用
setWrap
标志可以更轻松地完成任务。

您必须在构造函数的开头加载所有内容

private void loadTextures() {
    walkSheet = new  Texture(Gdx.files.internal("animation_sheet.png"));
    TextureRegion[][] tmp = TextureRegion.split(walkSheet, walkSheet.getWidth() / 
            FRAME_COLS, walkSheet.getHeight() / FRAME_ROWS);
    walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
    int index = 0;
    for (int i = 0; i < FRAME_ROWS; i++) {
            for (int j = 0; j < FRAME_COLS; j++) {
                    walkFrames[index++] = tmp[i][j];
            }
    }
    walkAnimation = new Animation(0.025f, walkFrames);

    stateTime = 0f;

    bobTexture = new  Texture(Gdx.files.internal("images/bob.png"));
    blockTexture = new Texture(Gdx.files.internal("images/block.png"));
}
在调用之前,不要忘记开始sprite批处理

  public void render() {
     spriteBatch.begin();
            drawBlocks();
            drawBob();
     spriteBatch.end();

        if (debug)
            drawDebug();
      }
完整代码可在我的博客中找到: 是的,这是可能的

可以将所有动画内容包装在一个组中。现在在表中插入该组,该表将管理该组的位置

希望这有帮助。

请随意使用: (按原样编码,无保修)

实现您自己的可以显示动画的演员:

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;

/**
 * nils honermann (zeg)
 * - no warranty, free to use
 */
public class GdxAnimationActor extends Actor {

    private Animation animation;

    public void dispose(){
        for(TextureRegion tr : animation.getKeyFrames()){
            tr.getTexture().dispose();
        }
        animation = null;
    }

    public GdxAnimationActor(Animation animation){
        this.animation=animation;
        TextureRegion first = animation.getKeyFrame(0f);
        setBounds(first.getRegionX(),first.getRegionY(),first.getRegionWidth(),first.getRegionHeight());
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        super.draw(batch, parentAlpha);
        // TODO: Should be multiplied with the actor's alpha, allowing a parent's alpha to affect all children.

        TextureRegion current = animation.getKeyFrame(elapsed/1000.0f/*millis into seconds float*/);
        batch.draw(current,getX(), getY(), getOriginX(), getOriginY(), getWidth(), getHeight(), getScaleX(), getScaleY(), getRotation());

        if(renderedOnce){
            elapsed += System.currentTimeMillis() - millisLastRender;
        }
        millisLastRender = System.currentTimeMillis();
        renderedOnce = true;
    }

    private float elapsed = 0;
    private boolean renderedOnce;
    private long millisLastRender = 0;
}
并将动画传递到其中

float fps = 0.1f; //1 texture per 100 millis
Array<TextureRegion> textureArray = new Array<>(3);
textureArray.size = 3;
textureArray.set( (0) , new TextureRegion(new Texture(/*assets*/"example1.png");) );
textureArray.set( (1) , new TextureRegion(new Texture(/*assets*/"example2.png");) );
textureArray.set( (2) , new TextureRegion(new Texture(/*assets*/"example3.png");) );
Animation animation = new Animation(fps,textureArray,Animation.PlayMode.LOOP);
GdxAnimationActor animationActor = new GdxAnimationActor(animation);
float fps=0.1f//每100毫秒1个纹理
数组textureArray=新数组(3);
textureArray.size=3;
textureArray.set((0),new TextureRegion(new Texture(/*assets*/“example1.png”););
textureArray.set((1),new TextureRegion(new Texture(/*assets*/“example2.png”););
textureArray.set((2),new TextureRegion(new Texture(/*assets*/“example3.png”););
动画=新动画(fps、textureArray、Animation.PlayMode.LOOP);
GdxAnimationActor animationActor=新的GdxAnimationActor(动画);

只需将animationActor添加到舞台、窗口或表格中即可。

我知道如何使用
SpriteBatch
绘制动画。我的问题是,是否可以继续使用这个桌子。但是这个团体也需要演员(动画不是这样的)。它与图像一起工作,因为图像扩展自Actor类,而不是动画。那么,我如何才能将动画添加到组中呢?您可以随时重写draw方法,这样您就可以访问spritebatch,而rest就很容易了。谢谢,这很有效:我最终创建了自己的
Actor
类,并重写了
draw()
方法来绘制动画,重写了
act(float delta)
方法来更新状态时间。
float fps = 0.1f; //1 texture per 100 millis
Array<TextureRegion> textureArray = new Array<>(3);
textureArray.size = 3;
textureArray.set( (0) , new TextureRegion(new Texture(/*assets*/"example1.png");) );
textureArray.set( (1) , new TextureRegion(new Texture(/*assets*/"example2.png");) );
textureArray.set( (2) , new TextureRegion(new Texture(/*assets*/"example3.png");) );
Animation animation = new Animation(fps,textureArray,Animation.PlayMode.LOOP);
GdxAnimationActor animationActor = new GdxAnimationActor(animation);