Libgdx Animation Sprite
Libgdx has it's own class Animation, however that is a basic animation. In the game I've developed some of the animation needs mode advanced frame ordering using a single spritesheet.
Here is the example of the animation spritesheet
And here is the frames of animation I want to accomplish
frame number is started from 0 (top left)
Walk frames : 0,1,2,3,4,3,2,1 (loop)
Attack frames : 5,6,7,7,7,7 --> the character stays on frame 7 for a while
Dizzy and sprawl : 17,18,19,18,17,18,19,18,13,14,15,16 (17 to 19 for dizzy animation, the rest is for Sprawling)
So I wrote my own class to handle that task, Because everyday I use scene2d, I decided to extend the Actor class, then please welcome ... ActorClip ...
Using the Class
Step 1:
Create an animation spritesheet that will be linked with the actor, it should have same cell size on every animation frame, see above example every cell has 100x100 size in px.
Step 2:
Your character/actor must extends the ActorClip class, In your extended ActorClip class, create the Clip object with the parameters are TextureRegion of the animation, and animation grid size
Character.java
Show Plain Text- package com.boontaran.example;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.graphics.Texture;
- import com.badlogic.gdx.graphics.g2d.TextureRegion;
- import com.boontaran.games.ActorClip;
- import com.boontaran.games.Clip;
- //define the spritesheet,
- // in this example it's 500 x 500 (each cells is 100x100 px) in image.png
- TextureRegion region = new TextureRegion(new Texture(Gdx.files.internal("assets/image.png")), 500, 500);
- //create the clip, grid/cell size is 100x100
- //assign the clip to this actor
- setClip(clip);
- clip.setFPS(12);
- //other things you may need to adjust ...
- //clipOffsetX = ...
- //clipOffsetY = ...
- //you can also monitor the clip state
- //called when animation reach every keyframe
- @Override
- public void onFrame(int num) {
- // ...
- }
- //called on every animation completed
- @Override
- public void onComplete() {
- // ... example :
- /*
- //reset state
- state = IDLE;
- */
- }
- });
- }
- //these methods will control the clip
- public void walk() {
- //attention!! you may add a guard to make sure the methods is just called once ..
- // like this :
- /*
- if(state == WALK) return;
- state = WALK;
- */
- //frames 0,1,2,3,4,3,2,1 loop
- clip.playFrames(new int[]{0,1,2,3,4,3,2,1}, true);
- }
- public void attack() {
- //frames 5,5,5,5,6,7,7,7,7 no loop
- clip.playFrames(new int[]{5,5,5,5,5,6,7,7,7,7}, false);
- }
- public void die() {
- clip.playFrames(new int[]{17,18,19,18,17,18,19,18,13,14,15,16}, false);
- }
- public void stand() {
- clip.singleFrame(2);
- }
- }
Step 3:
Done, now you have a custom actor with animation inside it and ready to be animated. Here is the example of the game screen using scene2d with the Actor Clip animation
Show Plain Text
- package com.boontaran.example;
- import com.badlogic.gdx.Gdx;
- import com.badlogic.gdx.Screen;
- import com.badlogic.gdx.graphics.GL20;
- import com.badlogic.gdx.scenes.scene2d.Stage;
- public class TestScreen implements Screen {
- private Stage stage;
- public TestScreen() {
- stage = new Stage();
- //create character, add to stage
- stage.addActor(character);
- character.setPosition(200, 200);
- //play the animation
- character.walk();
- //character.die();
- //character.attack();
- }
- // ... ....
- }