libgdx中Actor的动作

Rud*_*_TM 22 android action actor libgdx

我已经成为了我的演员,但我不清楚如何利用这些actionact方法.在基本的Javadoc之外,我还没有找到关于这些方法的好教程.

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

Lud*_*vik 73

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

LibGDX中有各种可用的操作可供您使用.他们是com.badlogic.gdx.scenes.scene2d.actions包装.我会说有3种动作:

  1. 动画动作
  2. 复合行动
  3. 其他行为

动画操作会修改actor的各种属性,例如位置,旋转,缩放和alpha.他们是:

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

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

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

其他行为:

  • 重复 - 重复给定动作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
Run Code Online (Sandbox Code Playgroud)

创建更复杂的动作序列的示例:

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
            );
Run Code Online (Sandbox Code Playgroud)

动画动作也让你指定Interpolator.有各种实现:

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

插值器Javadoc:插值器定义动画的变化率.这允许基本动画效果(alpha,缩放,平移,旋转)加速,减速等.要将插补器设置为您的动作:

action.setInterpolator(AccelerateDecelerateInterpolator.$());
Run Code Online (Sandbox Code Playgroud)

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

actor.action(yourAction);
Run Code Online (Sandbox Code Playgroud)

要实际执行为舞台上的actor定义的所有动作,您必须在render方法中调用stage.act(...):

stage.act(Gdx.graphics.getDeltaTime());
stage.draw();
Run Code Online (Sandbox Code Playgroud)


she*_*tan 12

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

脚步:

1.此处下载库
2.创建一个访问者类.你可以节省时间,从这里抓住我正在使用的那个.
3.在您的Game类中声明TweenManager

    public static TweenManager tweenManager;
Run Code Online (Sandbox Code Playgroud)


create方法中:

    tweenManager = new TweenManager();
Run Code Online (Sandbox Code Playgroud)


渲染方法中:

    tweenManager.update(Gdx.graphics.getDeltaTime());
Run Code Online (Sandbox Code Playgroud)


4.随意使用它.防爆.

使用弹性插值在1.5秒内将actor移动到位置(100,200):

Tween.to(actor, ActorAccesor.POSITION_XY, 1.5f)
     .target(100, 200)
     .ease(Elastic.INOUT)
     .start(tweenManager);
Run Code Online (Sandbox Code Playgroud)


创建复杂的动画序列:

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);
Run Code Online (Sandbox Code Playgroud)


更多细节在这里

更新:替换死链接


Dro*_*sta 11

这是使用类com.badlogic.gdx.math.Interpolation的有用链接.因此,例如,要创建具有效果的moveTo ation,您可以简单地使用:

myActor.addAction(Actions.moveTo(100, 200, 0.7f, Interpolation.bounceOut));
Run Code Online (Sandbox Code Playgroud)

如果将Actions类导入为static(必须手动设置):

import static com.badlogic.gdx.scenes.scene2d.actions.Actions.*;
Run Code Online (Sandbox Code Playgroud)

那么,你可以像这样使用你的行动:

myActor.addAction(moveTo(100, 200, 0.7f, Interpolation.bounceOut)); 
Run Code Online (Sandbox Code Playgroud)