Package com.badlogic.gdx.graphics.g2d
Class Animation<T>
- java.lang.Object
-
- com.badlogic.gdx.graphics.g2d.Animation<T>
-
public class Animation<T> extends java.lang.ObjectAn 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
TextureRegionsand would be specified as:Animation<TextureRegion> myAnimation = new Animation<TextureRegion>(...);
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classAnimation.PlayModeDefines possible playback modes for anAnimation.
-
Constructor Summary
Constructors Constructor Description Animation(float frameDuration, Array<? extends T> keyFrames)Constructor, storing the frame duration and key frames.Animation(float frameDuration, Array<? extends T> keyFrames, Animation.PlayMode playMode)Constructor, storing the frame duration and key frames.Animation(float frameDuration, T... keyFrames)Constructor, storing the frame duration and key frames.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description floatgetAnimationDuration()floatgetFrameDuration()TgetKeyFrame(float stateTime)Returns a frame based on the so called state time.TgetKeyFrame(float stateTime, boolean looping)Returns a frame based on the so called state time.intgetKeyFrameIndex(float stateTime)Returns the current frame number.T[]getKeyFrames()Returns the keyframes[] array where all the frames of the animation are stored.Animation.PlayModegetPlayMode()Returns the animation play mode.booleanisAnimationFinished(float stateTime)Whether the animation would be finished if played without looping (PlayMode#NORMAL), given the state time.voidsetFrameDuration(float frameDuration)Sets duration a frame will be displayed.protected voidsetKeyFrames(T... keyFrames)voidsetPlayMode(Animation.PlayMode playMode)Sets the animation play mode.
-
-
-
Constructor Detail
-
Animation
public Animation(float frameDuration, Array<? 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, Array<? 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 Detail
-
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 bysetPlayMode(PlayMode)method.- Parameters:
stateTime-- Returns:
- the frame of animation for the given state time.
-
getKeyFrameIndex
public int getKeyFrameIndex(float stateTime)
Returns the current frame number.- Parameters:
stateTime-- 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 animationAnimation.PlayModeto use.
-
isAnimationFinished
public boolean isAnimationFinished(float stateTime)
Whether the animation would be finished if played without looping (PlayMode#NORMAL), given the state time.- Parameters:
stateTime-- Returns:
- whether the animation is finished.
-
setFrameDuration
public void setFrameDuration(float frameDuration)
Sets duration a frame will be displayed.- Parameters:
frameDuration- in seconds
-
getFrameDuration
public float getFrameDuration()
- Returns:
- the duration of a frame in seconds
-
getAnimationDuration
public float getAnimationDuration()
- Returns:
- the duration of the entire animation, number of frames times frame duration, in seconds
-
-