Class Animation

java.lang.Object
cit244.api.image.Animation

public final class Animation extends Object
An immutable sequence of sprite frames played at a fixed frame rate.

Animations contain no playback state. The frameAt(double) method selects a sprite using elapsed time relative to the beginning of the animation.

For a looping animation synchronized with the sketch's clock:


 Sprite pose = walk.frameAt(f.time());
 

For animations that start, stop, or switch in response to events, use AnimationPlayer.

One-shot animations hold their last frame after finishing. Multiple objects can share the same animation.

  • Method Summary

    Modifier and Type
    Method
    Description
    double
    Returns the time required to display all frames once.
    frameAt(double seconds)
    Selects the sprite displayed at an animation-relative time.
    static Animation
    loop(double fps, Sprite... frames)
    Creates an animation that repeats after its last frame.
    static Animation
    once(double fps, Sprite... frames)
    Creates an animation that holds its final frame when finished.

    Methods inherited from class java.lang.Object

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

    • loop

      public static Animation loop(double fps, Sprite... frames)
      Creates an animation that repeats after its last frame.
      Parameters:
      fps - frames per second (positive)
      frames - sprite frames in playback order; at least one is required
      Returns:
      a looping animation
      Throws:
      IllegalArgumentException - if fps or the frame list is invalid
    • once

      public static Animation once(double fps, Sprite... frames)
      Creates an animation that holds its final frame when finished.
      Parameters:
      fps - frames per second (positive)
      frames - sprite frames in playback order; at least one is required
      Returns:
      a non-looping animation
      Throws:
      IllegalArgumentException - if fps or the frame list is invalid
    • duration

      public double duration()
      Returns the time required to display all frames once.
      Returns:
      one cycle's duration in seconds
    • frameAt

      public Sprite frameAt(double seconds)
      Selects the sprite displayed at an animation-relative time.

      For looping animations, time wraps around after each cycle. For one-shot animations, the final frame remains selected after duration(). Negative times select the first frame.

      This method does NOT track time or restart the animation!! For animation switching and event-triggered playback, use AnimationPlayer.

      Parameters:
      seconds - seconds elapsed since the animation began
      Returns:
      sprite displayed at the given time
      Throws:
      IllegalArgumentException - if seconds is NaN or infinite