由于LibGDX的更改,此答案正在过时。有关最新文档,请参见{{link1:scene2d wiki页面}}。
在LibGDX中,有各种可用的操作。它们位于com.badlogic.gdx.scenes.scene2d.actions
包中。我会说有3种类型的操作:
动画操作修改您的演员的各种属性,例如位置、旋转、缩放和alpha。它们是:
组合动作将多个动作合并为一个动作,有以下几种:
其他动作:
$
,它创建该操作的实例。
创建动画操作的示例: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
创建更复杂的动作序列示例:
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
);
动画操作还可以让您指定Interpolator
。有各种实现:
Interpolator Javadoc:插值器定义了动画的变化率。这允许基本的动画效果(alpha、scale、translate、rotate)加速、减速等。 要将插值器设置为您的操作:
action.setInterpolator(AccelerateDecelerateInterpolator.$());
当您的插值器准备好与动作配合时,您可以将该动作设置到您的演员中:
actor.action(yourAction);
为了实际执行在舞台上定义的所有演员动作,您必须在渲染方法中调用stage.act(...):
stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
您应该尝试使用通用缓动引擎。它易于使用且功能强大...并且它使阅读复杂的动画变得轻而易举,因为所有命令都可以链接。请参见下面的示例。
步骤:
1. 从here下载库。
2. 创建一个访问器类。您可以节省时间并从here中获取我正在使用的类。
3. 在您的游戏类中声明TweenManager。
public static TweenManager tweenManager;
tweenManager = new TweenManager();
tweenManager.update(Gdx.graphics.getDeltaTime());
4. 你可以随意使用它。例如:
在1.5秒内使用弹性插值将演员移动到位置(100,200):
Tween.to(actor, ActorAccesor.POSITION_XY, 1.5f)
.target(100, 200)
.ease(Elastic.INOUT)
.start(tweenManager);
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);
更多细节在这里
更新:替换了失效的链接
这里有一个关于Class使用的有用链接:com.badlogic.gdx.math.Interpolation。
例如,要创建一个带有效果的moveTo动作,您可以简单地使用:
myActor.addAction(Actions.moveTo(100, 200, 0.7f, Interpolation.bounceOut));
如果您将Actions类的导入设置为静态(必须手动设置):
import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
然后,您可以像这样使用您的操作:
myActor.addAction(moveTo(100, 200, 0.7f, Interpolation.bounceOut));