I have made my Actor, but I am unclear on how to take advantage of the action
and act
methods. Outside of the basic Javadoc, I have not found a go
This answer is being rendered obsolete because of changes in LibGDX. For up to date documentation see scene2d wiki page.
There are various available actions in LibGDX ready for you. They are in com.badlogic.gdx.scenes.scene2d.actions
package. I would say that there are 3 kinds of actions:
Animation actions modify various properties of your actor, such as location, rotation, scale and alpha. They are:
Composite actions combine multiple actions in one action, there are:
Other actions:
Every action has a static method $
which creates instance of that Action.
Example of creating animation actions:
MoveTo move = MoveTo.$(200, 200, 0.5f); //move Actor to location (200,200) in 0.5 s
RotateTo rotate = RotateTo.$(60, 0.5f); //rotate Actor to angle 60 in 0.5 s
Example of creating more complex action sequence:
Sequence sequence = Sequence.$(
MoveTo.$(200, 200, 0.5f), //move actor to 200,200
RotateTo.$(90, 0.5f), //rotate actor to 90°
FadeOut.$(0.5f), //fade out actor (change alpha to 0)
Remove.$() //remove actor from stage
);
Animation actions also let you specify Interpolator
. There are various implementations:
Interpolator Javadoc: An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated etc. To set interpolator to your action:
action.setInterpolator(AccelerateDecelerateInterpolator.$());
When you have your action with interpolator ready, then you set that action to your actor:
actor.action(yourAction);
To actually execute all actions defined for actors on stage, you have to call stage.act(...) in your render method:
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
You should give Universal Tween Engine a try. It's easy to use and really powerful... and it makes reading complex animations a walk in the park because all commands can be chained. See examples bellow.
Steps:
1. Download the library from here
2. Create an accesor class. You can save the time and grab the one I was using from here.
3. In your Game class declare the TweenManager
public static TweenManager tweenManager;
In the create method:
tweenManager = new TweenManager();
In the render method:
tweenManager.update(Gdx.graphics.getDeltaTime());
4. Use it however you want. Ex.
Move actor to position (100, 200) in 1.5 seconds with elastic interpolation:
Tween.to(actor, ActorAccesor.POSITION_XY, 1.5f)
.target(100, 200)
.ease(Elastic.INOUT)
.start(tweenManager);
Create a complex sequence of animations:
Timeline.createSequence()
// First, set all objects to their initial positions
.push(Tween.set(...))
.push(Tween.set(...))
.push(Tween.set(...))
// Wait 1s
.pushPause(1.0f)
// Move the objects around, one after the other
.push(Tween.to(...))
.push(Tween.to(...))
.push(Tween.to(...))
// Then, move the objects around at the same time
.beginParallel()
.push(Tween.to(...))
.push(Tween.to(...))
.push(Tween.to(...))
.end()
// And repeat the whole sequence 2 times
.repeatYoyo(2, 0.5f)
// Let's go!
.start(tweenManager);
More details here
UPDATE: replaced dead link
Here is a useful link for the uses of Class com.badlogic.gdx.math.Interpolation. So for example, to create a moveTo ation with an effect you could simply use:
myActor.addAction(Actions.moveTo(100, 200, 0.7f, Interpolation.bounceOut));
If you set your Actions class import as static (you have to set it manually):
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
then, you can use your actions like this:
myActor.addAction(moveTo(100, 200, 0.7f, Interpolation.bounceOut));