Class Animation<T>

java.lang.Object
arc.graphics.g2d.Animation<T>

public class Animation<T> extends Object

An Animation stores a list of objects representing an animated sequence, e.g. for running or jumping. Each object in the Animation is called a key frame, and multiple key frames make up the animation.

The animation's type is the class representing a frame of animation. For example, a typical 2D animation could be made up of TextureRegions and would be specified as:

Animation<TextureRegion> myAnimation = new Animation<TextureRegion>(...);

  • Constructor Details

    • Animation

      public Animation(float frameDuration, Seq<? extends T> keyFrames)
      Constructor, storing the frame duration and key frames.
      Parameters:
      frameDuration - the time between frames in seconds.
      keyFrames - the objects representing the frames. If this Array is type-aware, getKeyFrames() can return the correct type of array. Otherwise, it returns an Object[].
    • Animation

      public Animation(float frameDuration, Seq<? extends T> keyFrames, Animation.PlayMode playMode)
      Constructor, storing the frame duration and key frames.
      Parameters:
      frameDuration - the time between frames in seconds.
      keyFrames - the objects representing the frames. If this Array is type-aware, getKeyFrames() can return the correct type of array. Otherwise, it returns an Object[].
    • Animation

      public Animation(float frameDuration, T... keyFrames)
      Constructor, storing the frame duration and key frames.
      Parameters:
      frameDuration - the time between frames in seconds.
      keyFrames - the objects representing the frames.
  • Method Details

    • getKeyFrame

      public T getKeyFrame(float stateTime, boolean looping)
      Returns a frame based on the so called state time. This is the amount of seconds an object has spent in the state this Animation instance represents, e.g. running, jumping and so on. The mode specifies whether the animation is looping or not.
      Parameters:
      stateTime - the time spent in the state represented by this animation.
      looping - whether the animation is looping or not.
      Returns:
      the frame of animation for the given state time.
    • getKeyFrame

      public T getKeyFrame(float stateTime)
      Returns a frame based on the so called state time. This is the amount of seconds an object has spent in the state this Animation instance represents, e.g. running, jumping and so on using the mode specified by setPlayMode(PlayMode) method.
      Returns:
      the frame of animation for the given state time.
    • getKeyFrameIndex

      public int getKeyFrameIndex(float stateTime)
      Returns the current frame number.
      Returns:
      current frame number
    • getKeyFrames

      public T[] getKeyFrames()
      Returns the keyframes[] array where all the frames of the animation are stored.
      Returns:
      The keyframes[] field. This array is an Object[] if the animation was instantiated with an Array that was not type-aware.
    • setKeyFrames

      protected void setKeyFrames(T... keyFrames)
    • getPlayMode

      public Animation.PlayMode getPlayMode()
      Returns the animation play mode.
    • setPlayMode

      public void setPlayMode(Animation.PlayMode playMode)
      Sets the animation play mode.
      Parameters:
      playMode - The animation Animation.PlayMode to use.
    • isAnimationFinished

      public boolean isAnimationFinished(float stateTime)
      Whether the animation would be finished if played without looping (PlayMode#NORMAL), given the state time.
      Returns:
      whether the animation is finished.
    • getFrameDuration

      public float getFrameDuration()
      Returns:
      the duration of a frame in seconds
    • setFrameDuration

      public void setFrameDuration(float frameDuration)
      Sets duration a frame will be displayed.
      Parameters:
      frameDuration - in seconds
    • getAnimationDuration

      public float getAnimationDuration()
      Returns:
      the duration of the entire animation, number of frames times frame duration, in seconds