Class AnimatedSprite

java.lang.Object
com.codename1.gaming.Sprite
com.codename1.gaming.AnimatedSprite
All Implemented Interfaces:
PhysicsLinkable

public class AnimatedSprite extends Sprite

A Sprite that cycles through a sequence of frames over time.

The animation advances in #onUpdate(double), so adding it to a Scene whose Scene#update(double) is called every frame (or calling #onUpdate(double) directly) drives playback. By default it loops; set #setLooping(boolean) to false to stop on the last frame.

  • Constructor Details

    • AnimatedSprite

      public AnimatedSprite(Image[] frames, double secondsPerFrame)

      Creates an animated sprite from an explicit array of frames.

      Parameters
      • frames: the frame images, played in order

      • secondsPerFrame: how long each frame is shown, in seconds

    • AnimatedSprite

      public AnimatedSprite(SpriteSheet sheet, int[] frameIndices, double secondsPerFrame)

      Creates an animated sprite from frames pulled out of a SpriteSheet.

      Parameters
      • sheet: the source sprite sheet

      • frameIndices: the linear frame indices to play, in order

      • secondsPerFrame: how long each frame is shown, in seconds

  • Method Details

    • onUpdate

      protected void onUpdate(double deltaSeconds)
      Description copied from class: Sprite
      Per frame update hook. The default implementation does nothing; subclasses such as AnimatedSprite override it to advance over time. Scene#update(double) invokes this for every sprite it contains.
      Overrides:
      onUpdate in class Sprite
    • play

      public void play()
      Starts (or resumes) playback.
    • pause

      public void pause()
      Pauses playback, keeping the current frame.
    • stop

      public void stop()
      Stops playback and rewinds to the first frame.
    • isPlaying

      public boolean isPlaying()
    • isLooping

      public boolean isLooping()
    • setLooping

      public void setLooping(boolean looping)
    • getFrameDuration

      public double getFrameDuration()
    • setFrameDuration

      public void setFrameDuration(double secondsPerFrame)
    • getCurrentFrame

      public int getCurrentFrame()
      The index of the frame currently being shown.
    • setCurrentFrame

      public void setCurrentFrame(int index)
      Jumps to the given frame index.
    • getFrameCount

      public int getFrameCount()