Animating textures
An animation is a sequence of frames played one after another to create movement. In GameApp, an animation uses frames from spritesheets or atlases.
For example, if you have a walking character, each step could be a frame from a spritesheet. The animation could also consist of some of the frames of an atlas. By showing these frames in order, you get a walking animation.
Creating animations
You can create an animation in three ways:
- Using a whole spritesheet (all frames are used).
- Using a segment from an atlas (all frames with the same prefix).
- Adding specific frames manually (pick only certain rows and columns).
1. Using a whole spritesheet
// Create an animation named "heroWalkAnim" using all frames from the "heroWalk" spritesheet
// Each frame will be shown for 0.1 seconds
GameApp.addAnimationFromSpritesheet("heroWalkAnim", "heroWalk", 0.1f, true);
- "heroWalkAnim" is the animation name.
- "heroWalk" is the spritesheet name.
- 0.1f is the duration of each frame in seconds.
truemeans the animation will loop.
2. Using regions from an atlas
Suppose we want to create the walking animation for the robot. Since there are multiple regions with the same name (and different index), we can easily create an animation with it:
// Load the texture atlas
GameApp.addTextureAtlas("robot", "textures/robot.atlas");
// Create an animation and show the walk regions from the robot
GameApp.addAnimationFromAtlas("my-animation", "robot", "walk", 0.2f, true);
- "my-animation" is the animation name.
- "robot" is the atlas name.
- "walk" is the region name.
- 0.2f is the duration of each frame in seconds.
truemeans the animation will loop.
Adding frames manually
You can also create an animation by adding frames manually to the animation. These frames can be taken from atlases or spritesheets.
// Option 1: Add specific frames manually from an atlas
GameApp.addEmptyAnimation("example-animation-1", 0.15f, false);
GameApp.addAnimationFrameFromAtlas("example-animation-1", "robot", "walk_0");
GameApp.addAnimationFrameFromAtlas("example-animation-1", "robot", "idle");
GameApp.addAnimationFrameFromAtlas("example-animation-1", "robot", "attack_1");
// Option 2: Add specific frames manually from a spritesheet
GameApp.addEmptyAnimation("example-animation-2", 0.15f, false);
GameApp.addAnimationFrameFromSpritesheet("example-animation-2", "heroWalk", 0, 1);
GameApp.addAnimationFrameFromSpritesheet("example-animation-2", "heroWalk", 0, 2);
GameApp.addAnimationFrameFromSpritesheet("example-animation-2", "heroWalk", 0, 3);
Updating the animation
In your render(float delta) method, you will let the animation know that it needs to update. Based on the delta, the animation will pick the right frame for you. The animation is able to get the delta value itself, so you don't have to pass it.
Drawing animations
Animations can be drawn just like spritesheet frames (and textures). GameApp provides methods for position, scaling, rotation, and flipping.
Draw at a position
// Draw the current frame of "heroWalkAnim" at x=100, y=100
GameApp.drawAnimation("heroWalkAnim", 100, 100);
Draw with scaling
Draw with rotation and flipping
Draw centered
Similarly, there are also methods for drawing scaled, rotated and flipped from the center.
Resetting Animations
You can restart an animation from the first frame:
Or restart in reverse (play frames backward):
Is finished?
This method is only meaningful when you animation is not looping. Otherwise it is never finished.
if (GameApp.isAnimationFinished("heroAttackAnim")) {
System.out.println("Attack animation finished!");
}
Current frame index and total frames
If you want to have detailed information about which state the animation is in, you can get the current frame of the animation.
int currentFrame = GameApp.getAnimationCurrentFrameIndex("heroWalkAnim");
int totalFrames = GameApp.getAnimationFrameCount("heroWalkAnim");
Jump to a specific frame
Disposing Animations
When an animation is no longer needed:
This removes it from memory. Spritesheet textures remain, so you still need to dispose them seperately.
Example with spritesheets
public class HeroAnimationScreen extends ScalableGameScreen {
public HeroAnimationScreen() {
super(1280, 720);
}
@Override
public void show() {
// Load the same spritesheet as before (5 rows x 5 columns, each frame 100x100)
GameApp.addSpriteSheet("heroWalk", "textures/hero_walk.png", 100, 100);
// Create an animation using all frames of the spritesheet
// Each frame will be shown for 0.1 seconds, loop = true
GameApp.addAnimationFromSpritesheet("heroWalkAnim", "heroWalk", 0.1f, true);
// Create a manual animation (e.g., an attack sequence) with selected frames
GameApp.addEmptyAnimation("heroAttackAnim", 0.15f, false);
GameApp.addAnimationFrameFromSpritesheet("heroAttackAnim", "heroWalk", 0, 1);
GameApp.addAnimationFrameFromSpritesheet("heroAttackAnim", "heroWalk", 0, 2);
GameApp.addAnimationFrameFromSpritesheet("heroAttackAnim", "heroWalk", 0, 3);
}
@Override
public void render(float delta) {
super.render(delta);
// Update animations first
GameApp.updateAnimation("heroWalkAnim");
GameApp.updateAnimation("heroAttackAnim");
// Now lets render both animations
GameApp.startSpriteRendering();
// Draw the walking animation at x=100, y=100
GameApp.drawAnimation("heroWalkAnim", 100, 100);
// Draw the attack animation rotated 45 degrees and flipped horizontally
GameApp.drawAnimation("heroAttackAnim", 400, 200, 100, 100, 45, true, false);
GameApp.endSpriteRendering();
}
@Override
public void hide() {
// Dispose animations
GameApp.disposeAnimation("heroWalkAnim");
GameApp.disposeAnimation("heroAttackAnim");
// Dispose the spritesheet
GameApp.disposeSpritesheet("heroWalk");
}
}
Example with atlas
public class HeroAnimationScreen extends ScalableGameScreen {
public HeroAnimationScreen() {
super(1280, 720);
}
@Override
public void show() {
// Load the atlas (e.g., hero.atlas)
GameApp.addTextureAtlas("robot", "textures/robot.atlas");
// Create an animation using all atlas regions with prefix "walk_"
// Each frame will be shown for 0.1 seconds, loop = true
GameApp.addAnimationFromAtlas("robot-walking", "robot", "walk", 0.1f,true);
// Create a manual animation with selected atlas regions
GameApp.addEmptyAnimation("robot-strange", 0.15f, false);
GameApp.addAnimationFrameFromAtlas("robot-strange", "robot", "idle");
GameApp.addAnimationFrameFromAtlas("robot-strange", "robot", "cheer_0");
GameApp.addAnimationFrameFromAtlas("robot-strange", "robot", "attack_2");
}
@Override
public void render(float delta) {
super.render(delta);
// Update animations first
GameApp.updateAnimation("robot-walking");
GameApp.updateAnimation("robot-strange");
// Now let's render both animations
GameApp.startSpriteRendering();
// Draw the walking animation at x=100, y=100
GameApp.drawAnimation("robot-walking", 100, 100);
// Draw the attack animation rotated 45 degrees and flipped horizontally
GameApp.drawAnimation("robot-strange", 400, 200, 100, 100, 45, true, false);
GameApp.endSpriteRendering();
}
@Override
public void hide() {
// Dispose animations
GameApp.disposeAnimation("robot-walking");
GameApp.disposeAnimation("robot-strange");
// Dispose the atlas
GameApp.disposeTextureAtlas("robot");
}
}