Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

Using Actors and Actions in the LibGDX Game Development Framework

Tech May 18 3

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 of targetAction for timeSec seconds.
  • Forever (targetAction) — repeats targetAction indefinitely.
  • Parallel (actionA, actionB, …) — runs multiple actions simultaneously.
  • Repeat (targetAction, count) — executes targetAction repeatedly for count iterations.
  • 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 over timeSec.
  • FadeTo (targetAlpha, timeSec) — transitions opacity to targetAlpha within timeSec.
  • MoveTo (destX, destY, timeSec) — relocates actor to (destX, destY) smoothly in timeSec.
  • MoveBy (offsetX, offsetY, timeSec) — shifts actor by given offsets over timeSec.
  • RotateTo (angleDeg, timeSec) — rotates actor to specific angle over timeSec.
  • RotateBy (deltaAngleDeg, timeSec) — increments current rotation by deltaAngleDeg during timeSec.
  • ScaleTo (scaleX, scaleY, timeSec) — resizes actor to specified scale factors in timeSec.

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.

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.