Animation 将多个动画转换为一个对象动画

Animation 将多个动画转换为一个对象动画,animation,libgdx,Animation,Libgdx,我现在正试图在播放器类中使用我的动画表,但我对我的情况有些怀疑。我在文档()中讨论了2d动画,但它只显示了一种类型的动画,在这种情况下是行走动画。我的环境不同,我有三种类型的动画:空闲、跑步和攀爬。我想知道是否可以在一个唯一的动画对象中保存所有类型的动画,或者我必须为每个对象创建3个对象?我也试着在stackoverflow和google上找到相同的东西,但什么也没找到 这就是我对我的私有方法所做的loadAnimations(): private void loadAnimations() {

我现在正试图在播放器类中使用我的动画表,但我对我的情况有些怀疑。我在文档()中讨论了2d动画,但它只显示了一种类型的动画,在这种情况下是行走动画。我的环境不同,我有三种类型的动画:空闲、跑步和攀爬。我想知道是否可以在一个唯一的动画对象中保存所有类型的动画,或者我必须为每个对象创建3个对象?我也试着在stackoverflow和google上找到相同的东西,但什么也没找到

这就是我对我的私有方法所做的loadAnimations()

private void loadAnimations() {
    // Get the player animations sheet
    Texture playerSheet = Assets.manager.get(Assets.playersheet);

    // List of texture regions that hold the animations
    TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Put all textures from texture region to 1-d texture region
    int index = 0;
    TextureRegion[] walkFrames = new TextureRegion[PLAYER_FRAME_COLS * PLAYER_FRAME_ROWS];
    for (int i = 0; i < PLAYER_FRAME_ROWS; i++) {
        for (int j = 0; j < PLAYER_FRAME_COLS; j++) {
            walkFrames[index++] = tmp[i][j];
        }
    }
    this.runAnimation = new Animation(FRAMES_DURTION, walkFrames);
}

在这种情况下你该怎么办?谢谢。

为了利用动画类中的方法,我可能会创建3个单独的动画对象,每个“状态”(空闲、运行、爬升)对应一个。您拥有的代码(以及教程中的代码)将加载某个状态的动画纹理。因此,您需要对所有3个动画纹理执行类似的操作,以创建3个不同的动画对象

然后在渲染方法中,根据角色所处的“状态”,选择要渲染的动画

例如,在下面的行中

currentFrame = walkAnimation.getKeyFrame(stateTime, true);
在这里,您可以根据角色正在执行的操作选择要渲染的动画(idleAnimation、runAnimation、GrampAnimation)。(在动画之间切换时,您可能还需要使用stateTime值),但希望您理解我的建议

@Override
    public void render() {
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);                        
        stateTime += Gdx.graphics.getDeltaTime();           
        currentFrame = walkAnimation.getKeyFrame(stateTime, true);   
        spriteBatch.begin();
        spriteBatch.draw(currentFrame, 50, 50);             
        spriteBatch.end();
    }

我解决了我的问题。基本上我是这样做的:

这是我的动画变量:

// Player Animations
public static int PLAYER_FRAME_ROWS  =   3;
public static int PLAYER_FRAME_COLS  =   4;
public static float FRAMES_DURTION   = 0.13f;
public static enum ANIMATIONS_INDEX {IDLE, RUNNING, CLIMBING};
private Animation animations[];
private TextureRegion currentFrame;
private float stateTime, currentFrameDuration;
private int animationIndex;
在这里,在函数loadAnimations()中,我做了以下操作:

private void loadAnimations() {
    // Instance the number of types of animations
    this.animations = new Animation[ANIMATIONS_INDEX.values().length];

    // Get the player animations sheet
    Texture playerSheet = Assets.manager.get(Assets.playersheet);

    // List of texture regions that hold the animations
    TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Set the textures regions for each animation index
    for (int i = 0; i < ANIMATIONS_INDEX.values().length; i++) {
        this.animations[i] = new Animation(FRAMES_DURTION, tmp[i]);
    }

    // Set player bounds
    this.setBounds(this.getX(), this.getY(), playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Set state time to zero
    this.stateTime = 0;
}
private void loadAnimations(){
//实例动画的类型数
this.animations=新动画[animations_INDEX.values().length];
//获取播放器动画表
纹理playerseet=Assets.manager.get(Assets.playerseet);
//保存动画的纹理区域列表
TextureRegion[][]tmp=TextureRegion.split(playerSheet,playerSheet.getWidth()/playerSheet\u FRAME\u COLS,playerSheet.getHeight()/playerSheet\u FRAME\u ROWS);
//为每个动画索引设置纹理区域
对于(int i=0;i
private void loadAnimations() {
    // Instance the number of types of animations
    this.animations = new Animation[ANIMATIONS_INDEX.values().length];

    // Get the player animations sheet
    Texture playerSheet = Assets.manager.get(Assets.playersheet);

    // List of texture regions that hold the animations
    TextureRegion[][] tmp = TextureRegion.split(playerSheet, playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Set the textures regions for each animation index
    for (int i = 0; i < ANIMATIONS_INDEX.values().length; i++) {
        this.animations[i] = new Animation(FRAMES_DURTION, tmp[i]);
    }

    // Set player bounds
    this.setBounds(this.getX(), this.getY(), playerSheet.getWidth() / PLAYER_FRAME_COLS, playerSheet.getHeight() / PLAYER_FRAME_ROWS);

    // Set state time to zero
    this.stateTime = 0;
}