Class AnimationPlayer

java.lang.Object
cit244.api.image.AnimationPlayer

public final class AnimationPlayer extends Object
Plays an 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

    Constructors
    Constructor
    Description
    Creates a player at the beginning of an animation.
  • Method Summary

    Modifier and Type
    Method
    Description
    Returns the animation currently being played.
    boolean
    Reports whether the current one-shot animation has finished.
    Returns the current sprite without advancing playback.
    void
    play(Animation animation)
    Plays an animation, restarting only when switching to a different animation instance.
    void
    restart(Animation animation)
    Starts an animation from its first frame, even if it is already playing.
    update(double dt)
    Advances playback and returns the current sprite.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • AnimationPlayer

      public AnimationPlayer(Animation initial)
      Creates a player at the beginning of an animation.
      Parameters:
      initial - initial animation
      Throws:
      NullPointerException - if initial is null
  • Method Details

    • play

      public void play(Animation animation)
      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

      public void restart(Animation animation)
      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

      public Sprite update(double dt)
      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

      public Sprite 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

      public Animation current()
      Returns the animation currently being played.
      Returns:
      the current animation