1
votes

I'm trying now to use my animation sheet in the player class, but I'm having some doubts for my situation. I was following the documentation (https://github.com/libgdx/libgdx/wiki/2D-Animation) where it talks about 2d animation, but it just show a single type of animation, in that case, a walk animation. My context is different, I have 3 types of animation: idle, running and climbing. I wonder if it's possible hold all types of animation in a unique Animation object or I must create 3 objects for each? I also trie to find something equal on stackoverflow and google, but I found nothing.

This is what I did to my private method 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);
}

And this is the Player proprietes:

private Animation idleAnimation, runAnimation, climbAnimation;
private TextureRegion currentFrame;

What do to in this case? Thank you.

2

2 Answers

1
votes

To take advantage of the methods in the Animation class, I'd probably create 3 separate Animation objects, one for each "state" (idle, run, climb). The code you have (and in the tutorial) loads the animation textures for one of your states. So you'd need to do something similar with all 3 of your animation Textures to create the 3 different Animation objects.

Then in your render method, based on what "state" your character is in, you choose which animation to render.

ex, in the below, the line

currentFrame = walkAnimation.getKeyFrame(stateTime, true);

would be where you choose which animation (idleAnimation, runAnimation, climbAnimation) you'd render based on what your character is doing. (You may also need to play around with the stateTime values when switching between animations), but hopefully you understand what I'm suggesting.

@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();
    }
0
votes

I solved my problem. Basically I did this:

This is my variables for the animation:

// 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;

And here, on the function loadAnimations() I did this:

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;
}