libgdx中演员的行动

24
我已经创建了我的Actor,但我不清楚如何利用actionact方法。除了基本的Javadoc之外,我没有找到关于这些方法的好教程。
有人能提供一个带有注释的演员操作示例吗?
3个回答

82

由于LibGDX的更改,此答案正在过时。有关最新文档,请参见{{link1:scene2d wiki页面}}。

在LibGDX中,有各种可用的操作。它们位于com.badlogic.gdx.scenes.scene2d.actions包中。我会说有3种类型的操作:

  1. 动画操作
  2. 复合操作
  3. 其他操作

动画操作修改您的演员的各种属性,例如位置、旋转、缩放和alpha。它们是:

  • FadeIn - 将演员的透明度从当前透明度渐变到1
  • FadeOut - 将演员的透明度从当前透明度渐变到0
  • FadeTo - 将演员的透明度从当前透明度渐变到指定值
  • MoveBy - 将演员按照指定量移动
  • MoveTo - 将演员移动到指定位置
  • RotateBy - 将演员按照指定角度旋转
  • RotateTo - 将演员旋转到指定角度
  • ScaleTo - 将演员缩放到指定比例

组合动作将多个动作合并为一个动作,有以下几种:

  • Parallel - 并行执行给定的动作 - 所有动作同时进行
  • Sequence - 顺序执行给定的动作 - 一个接一个进行

其他动作:

  • Repeat - 重复执行给定的动作n次
  • Forever - 永久重复执行给定的动作
  • Delay - 延迟指定时间执行给定的动作
  • Remove - 从舞台上移除给定的演员
每个操作都有一个静态方法$,它创建该操作的实例。 创建动画操作的示例:
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。有各种实现:

  • AccelerateDecelerateInterpolator
  • AccelerateInterpolator
  • AnticipateInterpolator
  • DecelerateInterpolator
  • LinearInterpolator
  • OvershootInterpolator

Interpolator Javadoc:插值器定义了动画的变化率。这允许基本的动画效果(alpha、scale、translate、rotate)加速、减速等。 要将插值器设置为您的操作:

action.setInterpolator(AccelerateDecelerateInterpolator.$());

当您的插值器准备好与动作配合时,您可以将该动作设置到您的演员中:

actor.action(yourAction);

为了实际执行在舞台上定义的所有演员动作,您必须在渲染方法中调用stage.act(...):

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();

1
非常好的、全面的回答。 - P.T.
非常感谢您的答案 :) 解释得非常清楚。 - Rudy_TM

13

您应该尝试使用通用缓动引擎。它易于使用且功能强大...并且它使阅读复杂的动画变得轻而易举,因为所有命令都可以链接。请参见下面的示例。

步骤:

1.here下载库。
2. 创建一个访问器类。您可以节省时间并从here中获取我正在使用的类。
3. 在您的游戏类中声明TweenManager。

    public static TweenManager tweenManager;

create方法中:
    tweenManager = new TweenManager();

render 方法中:
    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);


更多细节在这里

更新:替换了失效的链接


11

这里有一个关于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)); 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接