Game Development Reference
In-Depth Information
How to do it...
To create a control that will handle the animations of a character, perform the following
steps:
1. Create a class called
CharacterAnimationManager
and have it extend
Ab-
stractControl
. This class should also implement
AnimEventListener
,
which
AnimControl
uses to tell our class when animations have finished play-
ing.
2. We're going to map Jaime's animations into an enum. This is so we don't have to
do a lot of string comparisons. While we're at it, we'll add some basic logic to the
enum as well. The name of the animation, whether the animation should loop or
not, and the time
AnimControl
should take to blend to a new animation using
the following code:
public enum Animation{
Idle(LoopMode.Loop, 0.2f),
Walk(LoopMode.Loop, 0.2f),
Run(LoopMode.Loop, 0.2f),
...
SideKick(LoopMode.DontLoop, 0.1f);
Animation(LoopMode loopMode, float blendTime){
this.loopMode = loopMode;
this.blendTime = blendTime;
}
LoopMode loopMode;
float blendTime;
}
We need two fields as well: an
AnimControl
field called
animControl
and
an
AnimChannel
called
mainChannel
.
3. We set these in the
setSpatial
method, as shown in the following code. Don't
forget to add the class to the
AnimControl
field as a listener, or we won't re-
ceive any calls when animations are finished:
public void setSpatial(Spatial spatial) {
super.setSpatial(spatial);
animControl = spatial.getControl(AnimControl.class);