我做了我的演员 ,但我不清楚如何利用action
和act
方法。 除了基本的Javadoc之外,我还没有find关于这些方法的很好的教程。
任何人都可以提供一个演员的行动评论的例子吗?
由于LibGDX的变化,这个答案已经过时了。 有关最新的文档,请参阅scene2d wiki页面 。
LibGDX有各种可用的操作。 他们在com.badlogic.gdx.scenes.scene2d.actions
包中。 我会说有三种行动:
animation动作修改你的actor的各种属性,比如位置,旋转,缩放和alpha。 他们是:
综合行动在一个行动中结合了多个行动,有:
其他行为:
每个动作都有一个静态方法$
,它创build该Action的实例。 创buildanimation动作的示例:
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
创build更复杂的动作序列的例子:
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动作还可以指定Interpolator
。 有各种实现:
内插器Javadoc:内插器定义animation的变化率。 这使得基本的animation效果(alpha,缩放,平移,旋转)得到加速,减速等。为你的动作设置插值器:
action.setInterpolator(AccelerateDecelerateInterpolator.$());
当你已经准备好了插补器的动作,那么你将这个动作设置给你的actor:
actor.action(yourAction);
要实际执行为舞台上的演员定义的所有动作,您必须在render方法中调用stage.act(…):
stage.act(Gdx.graphics.getDeltaTime()); stage.draw();
这里有一个有用的链接,用于类com.badlogic.gdx.math.Interpolation的使用。 因此,例如,要创build一个移动效果,您可以简单地使用:
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));
你应该试试通用吐温引擎。 它很容易使用,而且function非常强大,并且可以让复杂的animation在公园里散步,因为所有的命令都可以链接在一起。 见下面的例子。
脚步:
1.从这里下载库
2.创build一个accesor类。 你可以节省时间,并从这里抓住我正在使用的那个。
3.在你的Game类中声明TweenManager
public static TweenManager tweenManager;
在创build方法中:
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);
创build一个复杂的animation序列:
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);
更多细节在这里
更新:取代了死链接