Class AnimationPlayer
Animation for a character or other object, keeping
track of its elapsed playback time.
Each player has its own playback state, so multiple characters can share the same animation without playing in sync.
Create one player per character and call update(double)
once per frame:
// In setup, after creating the animations:
hero = new AnimationPlayer(idle);
// In render:
if (in.keyJustPressed(' ')) {
hero.restart(attack);
} else if (hero.current() != attack || hero.finished()) {
hero.play(in.keyDown('d') ? walk : idle);
}
g.sprite(hero.update(f.dt()).at(x, y));
Create animations once, rather than inside the render loop.
Constructing a new animation every frame causes play() to
restart playback repeatedly, making the sprite appear stuck on
its first frame.
-
Constructor Summary
ConstructorsConstructorDescriptionAnimationPlayer(Animation initial) Creates a player at the beginning of an animation. -
Method Summary
Modifier and TypeMethodDescriptioncurrent()Returns the animation currently being played.booleanfinished()Reports whether the current one-shot animation has finished.frame()Returns the current sprite without advancing playback.voidPlays an animation, restarting only when switching to a different animation instance.voidStarts an animation from its first frame, even if it is already playing.update(double dt) Advances playback and returns the current sprite.
-
Constructor Details
-
AnimationPlayer
Creates a player at the beginning of an animation.- Parameters:
initial- initial animation- Throws:
NullPointerException- if initial is null
-
-
Method Details
-
play
Plays an animation, restarting only when switching to a different animation instance.Repeated calls with the same instance leave playback uninterrupted, even if a one-shot animation has finished. Use
restart(Animation)to replay it.- Parameters:
animation- animation to play- Throws:
NullPointerException- if animation is null
-
restart
Starts an animation from its first frame, even if it is already playing.- Parameters:
animation- animation to restart- Throws:
NullPointerException- if animation is null
-
update
Advances playback and returns the current sprite.Looping animations repeat indefinitely. One-shot animations hold their final frame after finishing.
Skipping updates pauses playback. Passing a smaller time step, such as
f.dt() * 0.5, produces slow motion.- Parameters:
dt- elapsed time in seconds (nonnegative and finite)- Returns:
- the sprite after advancing playback
- Throws:
IllegalArgumentException- if dt is negative or non-finite
-
frame
Returns the current sprite without advancing playback.- Returns:
- the sprite at the current playback time
-
finished
public boolean finished()Reports whether the current one-shot animation has finished.Becomes true only after the last frame has been shown for its full duration, not when it first appears. Looping animations never finish.
- Returns:
- true if the current one-shot animation has finished
-
current
Returns the animation currently being played.- Returns:
- the current animation
-