Actions

Tutorial Objective

Demonstrate basic Actor Action using LibGDX Scene2D

  • MoveByAction
  • RotateAction
  • ScaleByAction
  • SequenceAction
  • ParallelAction

By the end of the tutorial you will have the following:

What you should be familiar with for this tutorial

What you will need to do this tutorial

Actor Action basic

You can add an Action to all type of Scene2D Actor: Button, Image, Group..

There is two ways to add an Action:

  • Shorthand: More compact but harder to read when combining actions
  • Full declaration: Easier to read but takes lot of space.

Shorthand

The following shorthand Action makes an object move to a given position:

anActor.addAction(Actions.moveTo(x,y,time));

Full declaration

Equivalent Action fully declared:

MoveToAction action = new MoveToAction();
action.setDuration(duration);
action.setPosition(x,y);
actor.addAction(action);

Code

Full Code available for download here or can be read below.

public class MyGdxGame extends ApplicationAdapter {
  private Stage stage;

  @Override
  public void create () {
    stage = new Stage(new ScreenViewport());
    Texture texture = new Texture(Gdx.files.absolute("image.jpg"));

    int X_left= Gdx.graphics.getWidth()/3-texture.getWidth()/2;
    int X_right = Gdx.graphics.getWidth()*2/3-texture.getWidth()/2;
    int Y_top = Gdx.graphics.getHeight()*2/3-texture.getHeight()/2;
    int Y_bottom = Gdx.graphics.getHeight()/3-texture.getHeight()/2;

    Image image1 = new Image(texture);
    image1.setPosition(X_left,Y_top);
    image1.setOrigin(image1.getWidth()/2,image1.getHeight()/2);
    stage.addActor(image1);

    ParallelAction topLeftRightParallelAction = new ParallelAction();
    topLeftRightParallelAction.addAction(Actions.moveTo(X_right,Y_top,1,Interpolation.exp5Out));
    topLeftRightParallelAction.addAction(Actions.scaleTo(2,2,1,Interpolation.exp5Out));

    MoveToAction moveBottomRightAction = new MoveToAction();
    moveBottomRightAction.setPosition(X_right,Y_bottom);
    moveBottomRightAction.setDuration(1);
    moveBottomRightAction.setInterpolation(Interpolation.smooth);

    ParallelAction bottomLeftRightParallelAction = new ParallelAction();
    bottomLeftRightParallelAction.addAction(Actions.moveTo(X_left,Y_bottom,1,Interpolation.sineOut));
    bottomLeftRightParallelAction.addAction(Actions.scaleTo(1,1,1));

    ParallelAction leftBottomTopParallelAction = new ParallelAction();
    leftBottomTopParallelAction.addAction(Actions.moveTo(X_left,Y_top,1,Interpolation.swingOut));
    leftBottomTopParallelAction.addAction(Actions.rotateBy(90,1));

    SequenceAction overallSequence = new SequenceAction();
    overallSequence.addAction(topLeftRightParallelAction);
    overallSequence.addAction(moveBottomRightAction);
    overallSequence.addAction(bottomLeftRightParallelAction);
    overallSequence.addAction(leftBottomTopParallelAction);

    RepeatAction infiniteLoop = new RepeatAction();
    infiniteLoop.setCount(RepeatAction.FOREVER);
    infiniteLoop.setAction(overallSequence);
image1.addAction(infiniteLoop);
  }
  @Override
  public void render () {
    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    stage.act();
stage.draw();
  }
}
Advertisement

5 thoughts on “Actions

  1. To make this work, instead of:

    Texture texture = new Texture(Gdx.files.absolute(“image.jpg”));

    I used:

    Texture texture = new Texture(Gdx.files.internal(“image.jpg”));

    Like

    1. Ah! Good point, this line (Declaring the Skin) is not required in this particular example.
      The Skin is used to show a Label text, it contains the information about the font.
      I remove it.
      Thanks for pointing it out!

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s