Skip to content

Animations

miguel edited this page Jan 8, 2019 · 14 revisions

Animations are already supported by libgdx natively. However, this implementation is too generic to fit into the braingdx framework. Instead, braingdx implements its own Animation Utility integrated with libgdx.

Animating GameObject types

Game Objects are part of the game world. All game objects are rendered into the game world via the GameObjectRenderer interface. Read here how this is done.

Given the following spritesheet:

SpriteSheet

As you can see the sprite sheet contains 8 different characters in total, each of them has 4 different animations for each direction. This is how you can animate the second character from the top left in braingdx:

/* in your AbstractScreen implementation */
// get the tileset as a texture, alternatively load it directly with libgdx
// The ID of your character
final int RPG_CHARACTER_ID = 1;
Texture texture = SharedAssetManager.getInstance().get(Assets.RPG.CHARACTER_TILESET);
// Convert the texture into a sprite sheet and describe how many tiles exist
AnimationSpriteSheet sheet = new AnimationSpriteSheet(texture, 32, 48);
// Describe how GameObjects of type 'RPG_CHARACTER_ID' should be rendered
context.getRenderManager().register(RPG_CHARACTER_ID, new AnimationRenderer(sheet,
   AnimationConfig.builder()
      .registerFrames(AnimationDrawable.DEFAULT_FRAME_ID, AnimationFrames.builder()
         // the number of frames
         .frames(3)
         // 4th tile from the left (3=index)
         .origin(3, 0)
         // change frame every 200m
         .duration(0.2f)
         // Animate directional
         .direction(Direction.HORIZONTAL)
         // Animate to the end and then backwards and repeat
         .playMode(Animation.PlayMode.LOOP_PINGPONG)
         .build())
      .build());

The result will be a moving character which walks down without moving.

Animating UI elements

For animating UI elements we can use the same API based on the Drawable class in libgdx:

Texture texture = SharedAssetManager.getInstance().get(Assets.UI_LOGO);
// Convert the texture into a sprite sheet and describe how many tiles exist
AnimationSpriteSheet sheet = new AnimationSpriteSheet(texture, 128, 32);
AnimationDrawable drawable = new AnimationDrawable(sheet,
   AnimationConfig.builder()
      .registerFrames(AnimationDrawable.DEFAULT_FRAME_ID, AnimationFrames.builder()
         // the number of frames
         .frames(3)
         // 1th tile from the left (3=index)
         .origin(0, 0)
         // change frame every 100m
         .duration(0.1f)
         // Animate directional
         .direction(Direction.HORIZONTAL)
         // Animate to the end and repeat
         .playMode(Animation.PlayMode.LOOP)
         .build())
      .build());
// add a new UI element to animate
context.getStage().addActor(new Image(drawable));
Clone this wiki locally