Package nl.saxion.gameapp.animation
Class SpriteAnimation
java.lang.Object
nl.saxion.gameapp.animation.Animatable<com.badlogic.gdx.graphics.g2d.TextureRegion>
nl.saxion.gameapp.animation.SpriteAnimation
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
ConstructorsConstructorDescriptionSpriteAnimation(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 TypeMethodDescriptionvoidaddFrame(com.badlogic.gdx.graphics.g2d.TextureRegion frame) Adds a single frame to the animation.com.badlogic.gdx.graphics.g2d.TextureRegionReturns the current frame of the animation based on elapsed time.intReturns the 0-based index of the current frame based on elapsed time.intReturns the total number of frames in this animation.booleanChecks whether the animation has finished.voidjumpToFrame(int frameIndex) Jumps the animation to a specific frame.voidreset()Resets the animation to the beginning without reversing (default behavior).voidreset(boolean reverse) Resets the animation to the beginning.com.badlogic.gdx.graphics.g2d.TextureRegionupdate(float deltaTime) Updates the animation's elapsed time.
-
Constructor Details
-
SpriteAnimation
public SpriteAnimation(float frameDuration, boolean loop) Creates a new, empty animation.- Parameters:
frameDuration- duration per frame in secondsloop- 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 framesframeDuration- duration per frame in secondsloop- 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:
updatein classAnimatable<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
TextureRegionframe
-
isFinished
public boolean isFinished()Checks whether the animation has finished.For looping animations, this always returns
false.- Specified by:
isFinishedin classAnimatable<com.badlogic.gdx.graphics.g2d.TextureRegion>- Returns:
trueif the animation is finished and not looping,falseotherwise
-
reset
public void reset(boolean reverse) Resets the animation to the beginning.If
reverseis true, the animation will play frames in reverse order.- Specified by:
resetin classAnimatable<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:
resetin classAnimatable<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
-