libgdx中Actor的动作

时间:2012-02-03 15:58:48

标签: android action actor libgdx

我已经制作了Actor,但我不清楚如何利用actionact方法。在基本的Javadoc之外,我还没有找到关于这些方法的好教程。

任何人都可以提供一个关于演员行动评论的例子吗?

3 个答案:

答案 0 :(得分:72)

由于LibGDX的变化,这个答案已经过时了。有关最新文档,请参阅scene2d wiki page

LibGDX中有各种可用的操作可供您使用。它们位于com.badlogic.gdx.scenes.scene2d.actions包中。我会说有3种行为:

  1. 动画动作
  2. 复合行动
  3. 其他行动
  4. 动画操作可修改actor的各种属性,例如位置,旋转,缩放和alpha。他们是:

    • FadeIn - 将演员的alpha从演员当前的alpha更改为1
    • FadeOut - 将演员的alpha从演员当前的alpha更改为0
    • FadeTo - 将演员的alpha从演员当前的alpha更改为特定值
    • MoveBy - 将您的演员移动特定金额
    • MoveTo - 将您的演员移动到特定位置
    • RotateBy - 按特定角度
    • 旋转您的演员
    • RotateTo - 将您的演员旋转到特定角度
    • ScaleTo - 将您的演员缩放到特定比例因子

    复合动作在一个动作中组合了多个动作,有:

    • 并行 - 并行执行给定的操作 - 一次执行所有操作
    • 序列 - 按顺序执行给定的动作 - 一个接一个地执行

    其他行动:

    • 重复 - 重复给定动作n次
    • 永远 - 永远重复行动
    • 延迟 - 在特定时间内延迟执行特定操作
    • 删除 - 从舞台
    • 中删除给定的Actor

    每个操作都有一个静态方法$,它创建该Action的实例。 创建动画操作的示例:

    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,缩放,平移,旋转)加速,减速等。 要将插补器设置为您的操作:

    action.setInterpolator(AccelerateDecelerateInterpolator.$());
    

    当你准备好插补器的动作时,你就可以将动作设置为你的演员:

    actor.action(yourAction);
    

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

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

答案 1 :(得分:12)

您应该尝试使用Universal Tween Engine。它易于使用且功能强大......它使得阅读复杂的动画在公园散步,因为所有命令都可以链接。见下文的例子。

<强>步骤:

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

    public static TweenManager tweenManager;


创建方法中:

    tweenManager = new TweenManager();


渲染方法中:

    tweenManager.update(Gdx.graphics.getDeltaTime());


4. 尽可能使用它。例

使用弹性插值在1.5秒内将actor移动到位置(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);


更多详情here

更新:替换死链接

答案 2 :(得分:11)

以下是使用类com.badlogic.gdx.math.Interpolation的有用链接。 因此,例如,要创建具有效果的moveTo ation,您只需使用:

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));