Example usage for com.badlogic.gdx.graphics.g2d Animation getKeyFrames

List of usage examples for com.badlogic.gdx.graphics.g2d Animation getKeyFrames

Introduction

In this page you can find the example usage for com.badlogic.gdx.graphics.g2d Animation getKeyFrames.

Prototype

public TextureRegion[] getKeyFrames() 

Source Link

Document

Returns the keyFrames[] array where all the TextureRegions of the animation are stored.

Usage

From source file:com.github.fauu.helix.graphics.AnimationSet.java

License:Open Source License

public Animation get(AnimationType type, Direction direction, float duration) {
    Animation animation = animations.get(type).get(direction);

    animation.setFrameDuration(duration / animation.getKeyFrames().length);

    return animation;
}

From source file:com.ruin.castile.chara.AnimationSet.java

License:Open Source License

private void load(String name) {
    FileHandle file = Gdx.files.internal(DIRECTORY_NAME + "/" + name + "." + EXTENSION);

    TextureAtlas atlas = new TextureAtlas(Gdx.files.internal("packedimages/" + "game" + ".atlas"));

    Gson gson = new Gson();

    JsonObject object = new JsonParser().parse(file.reader()).getAsJsonObject();

    JsonArray entries = object.get("anims").getAsJsonArray();

    for (int index = 0; index < entries.size(); index++) {
        JsonObject animObject = entries.get(index).getAsJsonObject();

        AnimationType type = AnimationType.valueOf(animObject.get("name").getAsString());

        JsonObject directions = animObject.get("directions").getAsJsonObject();

        for (Direction dir : Direction.values()) {

            Direction theDir = dir;/*w  w w  .  j  av  a  2 s  .  co  m*/
            if (dir.shouldFlipSprite())
                theDir = dir.getReverseSpriteDirection();

            JsonElement entr = directions.get(theDir.toString());

            if (entr == null)
                continue;

            JsonObject elements = entr.getAsJsonObject();

            String[] frameNames = gson.fromJson(elements.get("frames"), String[].class);
            int[][] offsets = gson.fromJson(elements.get("offsets"), int[][].class);
            TextureRegion[] frames = new TextureRegion[frameNames.length];

            for (int i = 0; i < frameNames.length; i++) {
                frames[i] = atlas.findRegion(frameNames[i]);
                if (dir.shouldFlipSprite()) {
                    frames[i] = new TextureRegion(frames[i]);
                    frames[i].flip(true, false);
                    frames[i].setRegionX(frames[i].getRegionX() + offsets[i][1]);
                    frames[i].setRegionY(frames[i].getRegionY() + offsets[i][0]);
                    frames[i].setRegionWidth(frames[i].getRegionWidth() + offsets[i][1]);
                    frames[i].setRegionHeight(frames[i].getRegionHeight() + offsets[i][0]);
                } else {
                    frames[i].setRegionX(frames[i].getRegionX() + offsets[i][0]);
                    frames[i].setRegionY(frames[i].getRegionY() + offsets[i][1]);
                    frames[i].setRegionWidth(frames[i].getRegionWidth() + offsets[i][0]);
                    frames[i].setRegionHeight(frames[i].getRegionHeight() + offsets[i][1]);
                }
            }

            Animation animation = new Animation(1, frames);

            animation.setFrameDuration(1.0f / animation.getKeyFrames().length);

            add(type, dir, animation);
        }
    }
}