Class Animation

  • All Implemented Interfaces:
    Drawable<com.badlogic.gdx.graphics.g2d.Batch>

    public final class Animation
    extends java.lang.Object
    implements Drawable<com.badlogic.gdx.graphics.g2d.Batch>
    Animated or static graphical representation of game objects (actors).

    Loads a sprite sheet from resource image file. Constructor parameters spriteWidth and spriteHeight should specify sprite dimensions. If the resource file dimensions are larger, they must be multiples of sprite's dimensions. The resource image will be automatically split into frames.

    Single-frame animations can be created with constructor with single parameter, with sprite dimensions automatically set to the dimensions of the resource.

    For animated resources (multiple frames), frame duration defines how long one frame is displayed, and play mode defines how the next frame to show is selected (see enum Animation.PlayMode for options).

    Additional parameters allow to define:

    • colored tint that should be applied to the animation
    • scale to resize the animation (relative to the original size), and
    • rotation of the animation (in degrees).
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  Animation.PlayMode
      Play mode defines a method for selecting next animation frame.
    • Constructor Summary

      Constructors 
      Constructor Description
      Animation​(@NotNull java.lang.String resource)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight, float frameDuration)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight, float frameDuration, @NotNull Animation.PlayMode playmode)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight, float frameDuration, @NotNull Animation.PlayMode playmode, @NotNull Color tint)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight, float frameDuration, @NotNull Animation.PlayMode playmode, @NotNull Color tint, float scale)  
      Animation​(@NotNull java.lang.String resource, int spriteWidth, int spriteHeight, float frameDuration, @NotNull Animation.PlayMode playMode, @NotNull Color tint, float scale, float rotation)  
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void draw​(@NotNull com.badlogic.gdx.graphics.g2d.Batch batch, float posX, float posY)  
      void draw​(@NotNull com.badlogic.gdx.graphics.g2d.Batch batch, float posX, float posY, float deltaTime)
      Draws the animation to the given batch at [posX, posY] coordinates.
      float getAlpha()
      Alpha transparency applied to the animation.
      int getCurrentFrameIndex()
      Index of currently shown animation frame.
      int getFrameCount()
      Count of animation frames.
      float getFrameDuration()
      Duration of a single animation frame.
      int getHeight()
      Height of the animation object (sprite height).
      @NotNull Animation.PlayMode getPlayMode()
      Playing mode of the animation, one of Animation.PlayMode enum values.
      @NotNull java.lang.String getResource()  
      float getRotation()
      Rotation of the animation in degrees.
      float getScale()
      Scale of the animation, relative to the sprite image size.
      @NotNull Color getTint()
      A colored tint applied over animation to change its appearance.
      int getWidth()
      Width of the animation object (sprite width).
      void pause()
      Pauses the animation, causing it to keep rendering the current frame.
      void play()
      Starts to play or continues to play the animation according to the play mode.
      void resetToFirstFrame()
      Resets the animation to its first frame.
      void setAlpha​(float alpha)  
      void setFrameDuration​(float frameDuration)  
      void setPlayMode​(@NotNull Animation.PlayMode playMode)  
      void setRotation​(float rotation)  
      void setScale​(float scale)  
      void setTint​(@NotNull Color tint)  
      void stop()
      Stops playing animation and shows its first frame.
      java.lang.String toString()  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight,
                         float frameDuration)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight,
                         float frameDuration,
                         @NotNull
                         @NotNull Animation.PlayMode playmode)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight,
                         float frameDuration,
                         @NotNull
                         @NotNull Animation.PlayMode playmode,
                         @NotNull
                         @NotNull Color tint)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight,
                         float frameDuration,
                         @NotNull
                         @NotNull Animation.PlayMode playmode,
                         @NotNull
                         @NotNull Color tint,
                         float scale)
      • Animation

        public Animation​(@NotNull
                         @NotNull java.lang.String resource,
                         int spriteWidth,
                         int spriteHeight,
                         float frameDuration,
                         @NotNull
                         @NotNull Animation.PlayMode playMode,
                         @NotNull
                         @NotNull Color tint,
                         float scale,
                         float rotation)
    • Method Detail

      • getResource

        @NotNull
        public @NotNull java.lang.String getResource()
      • getWidth

        public int getWidth()
        Width of the animation object (sprite width).
      • getHeight

        public int getHeight()
        Height of the animation object (sprite height).
      • setPlayMode

        public void setPlayMode​(@NotNull
                                @NotNull Animation.PlayMode playMode)
      • getTint

        @NotNull
        public @NotNull Color getTint()
        A colored tint applied over animation to change its appearance.
      • setTint

        public void setTint​(@NotNull
                            @NotNull Color tint)
      • getScale

        public float getScale()
        Scale of the animation, relative to the sprite image size.
      • setScale

        public void setScale​(float scale)
      • getRotation

        public float getRotation()
        Rotation of the animation in degrees.
      • setRotation

        public void setRotation​(float rotation)
      • getAlpha

        public float getAlpha()
        Alpha transparency applied to the animation.
      • setAlpha

        public void setAlpha​(float alpha)
      • getFrameDuration

        public float getFrameDuration()
        Duration of a single animation frame.
      • setFrameDuration

        public void setFrameDuration​(float frameDuration)
      • getFrameCount

        public int getFrameCount()
        Count of animation frames.
      • getCurrentFrameIndex

        public int getCurrentFrameIndex()
        Index of currently shown animation frame.
      • play

        public void play()
        Starts to play or continues to play the animation according to the play mode.
      • pause

        public void pause()
        Pauses the animation, causing it to keep rendering the current frame.
      • resetToFirstFrame

        public void resetToFirstFrame()
        Resets the animation to its first frame. The animation will continue to play, if it was not in a stopped state.
      • stop

        public void stop()
        Stops playing animation and shows its first frame.

        A convenience method combining the pause() and resetToFirstFrame() methods.

      • draw

        public void draw​(@NotNull
                         @NotNull com.badlogic.gdx.graphics.g2d.Batch batch,
                         float posX,
                         float posY)
        Specified by:
        draw in interface Drawable<com.badlogic.gdx.graphics.g2d.Batch>
      • draw

        public void draw​(@NotNull
                         @NotNull com.badlogic.gdx.graphics.g2d.Batch batch,
                         float posX,
                         float posY,
                         float deltaTime)
        Draws the animation to the given batch at [posX, posY] coordinates.
        Specified by:
        draw in interface Drawable<com.badlogic.gdx.graphics.g2d.Batch>
        Parameters:
        deltaTime - represents the time passed from the previous render.
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object