Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

android - Actions of Actors in libgdx

I have made my Actor, but I am unclear on how to take advantage of the action and act methods. Outside of the basic Javadoc, I have not found a good tutorials on these methods.

Can anyone provide an example with comments for actions on actors?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

This answer is being rendered obsolete because of changes in LibGDX. For up to date documentation see scene2d wiki page.

There are various available actions in LibGDX ready for you. They are in com.badlogic.gdx.scenes.scene2d.actions package. I would say that there are 3 kinds of actions:

  1. Animation actions
  2. Composite actions
  3. Other actions

Animation actions modify various properties of your actor, such as location, rotation, scale and alpha. They are:

  • FadeIn - changes alpha of your actor from actor's current alpha to 1
  • FadeOut - changes alpha of your actor from actor's current alpha to 0
  • FadeTo - changes alpha of your actor from actor's current alpha to specific value
  • MoveBy - moves your actor by specific amount
  • MoveTo - moves your actor to specific location
  • RotateBy - rotates your actor by specific angle
  • RotateTo - rotates your actor to specific angle
  • ScaleTo - scales your actor to specific scale factor

Composite actions combine multiple actions in one action, there are:

  • Parallel - execute given actions in parallel - all actions at once
  • Sequence - execute given actions in sequence - one after another

Other actions:

  • Repeat - repeats given action n-times
  • Forever - repeats given action forever
  • Delay - delays execution of given action for specific amount of time
  • Remove - removes given Actor from stage

Every action has a static method $ which creates instance of that Action. Example of creating animation actions:

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

Example of creating more complex action sequence:

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 actions also let you specify Interpolator. There are various implementations:

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

Interpolator Javadoc: An interpolator defines the rate of change of an animation. This allows the basic animation effects (alpha, scale, translate, rotate) to be accelerated, decelerated etc. To set interpolator to your action:

action.setInterpolator(AccelerateDecelerateInterpolator.$());

When you have your action with interpolator ready, then you set that action to your actor:

actor.action(yourAction);

To actually execute all actions defined for actors on stage, you have to call stage.act(...) in your render method:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...