Class SpriteAnimation

java.lang.Object
nl.saxion.gameapp.animation.Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
nl.saxion.gameapp.animation.SpriteAnimation

public class SpriteAnimation extends Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
Represents a single sprite-based animation.

This class stores a sequence of TextureRegion frames and allows updating the current frame over time, looping, and querying completion.

Frames can be added all at once at construction, or manually added later using addFrame(TextureRegion).

  • Constructor Summary

    Constructors
    Constructor
    Description
    SpriteAnimation(float frameDuration, boolean loop)
    Creates a new, empty animation.
    SpriteAnimation(com.badlogic.gdx.graphics.g2d.TextureRegion[] initialFrames, float frameDuration, boolean loop)
    Creates a new animation with the specified initial frames.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    addFrame(com.badlogic.gdx.graphics.g2d.TextureRegion frame)
    Adds a single frame to the animation.
    com.badlogic.gdx.graphics.g2d.TextureRegion
    Returns the current frame of the animation based on elapsed time.
    int
    Returns the 0-based index of the current frame based on elapsed time.
    int
    Returns the total number of frames in this animation.
    boolean
    Checks whether the animation has finished.
    void
    jumpToFrame(int frameIndex)
    Jumps the animation to a specific frame.
    void
    Resets the animation to the beginning without reversing (default behavior).
    void
    reset(boolean reverse)
    Resets the animation to the beginning.
    com.badlogic.gdx.graphics.g2d.TextureRegion
    update(float deltaTime)
    Updates the animation's elapsed time.

    Methods inherited from class java.lang.Object

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

    • SpriteAnimation

      public SpriteAnimation(float frameDuration, boolean loop)
      Creates a new, empty animation.
      Parameters:
      frameDuration - duration per frame in seconds
      loop - whether the animation should loop
    • SpriteAnimation

      public SpriteAnimation(com.badlogic.gdx.graphics.g2d.TextureRegion[] initialFrames, float frameDuration, boolean loop)
      Creates a new animation with the specified initial frames.
      Parameters:
      initialFrames - the array of initial frames
      frameDuration - duration per frame in seconds
      loop - whether the animation should loop
  • Method Details

    • addFrame

      public void addFrame(com.badlogic.gdx.graphics.g2d.TextureRegion frame)
      Adds a single frame to the animation.
      Parameters:
      frame - the frame to add
    • update

      public com.badlogic.gdx.graphics.g2d.TextureRegion update(float deltaTime)
      Updates the animation's elapsed time.

      This should be called every frame with the delta time since the last update.

      Specified by:
      update in class Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
      Parameters:
      deltaTime - time in seconds since the last update
    • getCurrentFrame

      public com.badlogic.gdx.graphics.g2d.TextureRegion getCurrentFrame()
      Returns the current frame of the animation based on elapsed time.
      Returns:
      the current TextureRegion frame
    • isFinished

      public boolean isFinished()
      Checks whether the animation has finished.

      For looping animations, this always returns false.

      Specified by:
      isFinished in class Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
      Returns:
      true if the animation is finished and not looping, false otherwise
    • reset

      public void reset(boolean reverse)
      Resets the animation to the beginning.

      If reverse is true, the animation will play frames in reverse order.

      Specified by:
      reset in class Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
      Parameters:
      reverse - whether to reverse the frame order
    • reset

      public void reset()
      Resets the animation to the beginning without reversing (default behavior).
      Overrides:
      reset in class Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
    • getFrameCount

      public int getFrameCount()
      Returns the total number of frames in this animation.
      Returns:
      the number of frames
    • getCurrentFrameIndex

      public int getCurrentFrameIndex()
      Returns the 0-based index of the current frame based on elapsed time.
      Returns:
      the current frame index
    • jumpToFrame

      public void jumpToFrame(int frameIndex)
      Jumps the animation to a specific frame.

      Sets the current frame and updates the internal elapsed time accordingly.

      Parameters:
      frameIndex - the 0-based index of the frame to jump to
      Throws:
      IndexOutOfBoundsException - if the frame index is invalid