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 a Stage is and how to add an Image to it (See basic Image Tutorial)
What you will need to do this tutorial
- Install LibGDX & Create Sample Application (See getting started instructions)
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(); } }
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”));
LikeLike
I confirm that that chage is needed to run the application.
LikeLike
what error do you get?
LikeLike
Can someone explain to me what the skin (line 7 of the source code) does?
LikeLike
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!
LikeLike