Using Actors and Actions in the LibGDX Game Development Framework
In LibGDX, a actor’s rendering position is determined by its x and y coordinates, which can be manipulated directly. How ever, richer visual effects require combining actors with the Action classes located in com.badlogic.gdx.scenes.scene2d.actions.
Action types fall into two functional groups: controlling actions and performing actions. Controlling actions manage the execution of other actions without producing direct visual output, whereas performing actions inherit from AnimationAction and apply visible changes to an actor.
Controlling Actions
These actions orchestrate the timing and order of performing actions:
- Delay
(targetAction, timeSec)— postpones execution oftargetActionfortimeSecseconds. - Forever
(targetAction)— repeatstargetActionindefinitely. - Parallel
(actionA, actionB, …)— runs multiple actions simultaneously. - Repeat
(targetAction, count)— executestargetActionrepeatedly forcountiterations. - Sequence
(firstAction, secondAction, …)— triggers actions one after another. - Remove
()— clears all actions attached to an actor.
Performing Actions
These directly alter a actor’s appearance or transform properties:
- FadeIn
(timeSec)/ FadeOut(timeSec)— gradually changes opacity to fully visible or invisible overtimeSec. - FadeTo
(targetAlpha, timeSec)— transitions opacity totargetAlphawithintimeSec. - MoveTo
(destX, destY, timeSec)— relocates actor to (destX,destY) smoothly intimeSec. - MoveBy
(offsetX, offsetY, timeSec)— shifts actor by given offsets overtimeSec. - RotateTo
(angleDeg, timeSec)— rotates actor to specific angle overtimeSec. - RotateBy
(deltaAngleDeg, timeSec)— increments current rotation bydeltaAngleDegduringtimeSec. - ScaleTo
(scaleX, scaleY, timeSec)— resizes actor to specified scale factors intimeSec.
Combining these actions enables complex choreography. For example, simultaneous fading, motion, and rotation can be achieved via parallel composition.
Below is a compact demonstration spawning multiple actors with blended animations:
package demo.libgdx.actions;
import com.badlogic.gdx.*;
import com.badlogic.gdx.graphics.*;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Random;
import com.badlogic.gdx.scenes.scene2d.*;
import com.badlogic.gdx.scenes.scene2d.actions.*;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
public class ActionDemo implements ApplicationListener {
private Stage mainStage;
private Texture spriteSheet;
@Override
public void create() {
mainStage = new Stage(Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
spriteSheet = new Texture(Gdx.files.internal("star.png"));
spriteSheet.setFilter(Texture.TextureFilter.Linear, Texture.TextureFilter.Linear);
float span = 4.0f;
int screenW = Gdx.graphics.getWidth();
int screenH = Gdx.graphics.getHeight();
int spriteW = spriteSheet.getWidth();
int spriteH = spriteSheet.getHeight();
int boundX = screenW - spriteW;
int boundY = screenH - spriteH;
Random rand = new Random();
for (int idx = 0; idx < 20; idx++) {
Image starImg = new Image(new TextureRegion(spriteSheet));
starImg.setName("star_" + idx);
starImg.setPosition(rand.nextInt(boundX), rand.nextInt(screenH));
Action travel = Sequence.$(
MoveTo.$(rand.nextInt(boundX), rand.nextInt(boundY), span / 2),
MoveBy.$(rand.nextInt(boundX), rand.nextInt(boundY), span / 2)
);
Action spin = RotateTo.$(360, span);
Action twinkle = Repeat.$(
Sequence.$(FadeOut.$(span / 20), FadeIn.$(span / 20)),
10
);
starImg.addAction(Parallel.$(travel, spin, twinkle));
mainStage.addActor(starImg);
}
Gdx.input.setInputProcessor(mainStage);
}
@Override
public void dispose() {
spriteSheet.dispose();
mainStage.dispose();
}
@Override
public void pause() {}
@Override
public void render() {
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
mainStage.act(Gdx.graphics.getDeltaTime());
mainStage.draw();
}
@Override
public void resize(int w, int h) {}
@Override
public void resume() {}
}
The core challenge lies in structuring sequences and parallels to match desired visual behavior. Coordinating delays, repetitions, and simultaneous transforms unlocks expressive motion design within LibGDX stages.