Class AnimatedSprite

java.lang.Object
org.openpatch.scratch.Sprite
org.openpatch.scratch.extensions.animation.AnimatedSprite

public class AnimatedSprite extends Sprite
The AnimatedSprite class represents a sprite that can play animations. It extends the Sprite class and provides methods to add animations, play animations, set the interval between animation frames, and reset the animation.

Example usage:


 AnimatedSprite sprite = new AnimatedSprite();
 sprite.addCostume("idle", "assets/idle.png");
 sprite.addAnimation("walk", "assets/walk_%d.png", 4);
 sprite.playAnimation("walk");
 
See Also:
  • Constructor Details

    • AnimatedSprite

      public AnimatedSprite()
    • AnimatedSprite

      public AnimatedSprite(AnimatedSprite animatedSprite)
  • Method Details

    • addAnimation

      public void addAnimation(String name, String pattern, int frames)
      Adds an animation to the sprite.
      Parameters:
      name - the name of the animation
      pattern - the pattern (@see String#format) of the file names. The frame starts from 1.
      frames - the number of frames in the animation
    • addAnimation

      public void addAnimation(String name, Function<Integer,String> builder, int frame)
      Adds an animation to the sprite.
      Parameters:
      name - the name of the animation
      builder - a function that builds the file names for the animation frames
      frame - the number of frames in the animation. The frame starts from 1.
    • addAnimation

      public void addAnimation(String name, String path, int frames, int width, int height)
      Adds an animation to the sprite.
      Parameters:
      name - the name of the animation
      path - the path to the animation frames
      frames - the number of frames in the animation
      width - the width of each frame
      height - the height of each frame
    • addAnimation

      public void addAnimation(String name, String path, int frames, int width, int height, int row)
      Adds an animation to the sprite.
      Parameters:
      name - the name of the animation
      path - the path to the animation frames
      frames - the number of frames in the animation
      width - the width of each frame
      height - the height of each frame
      row - the row of the animation frames
    • addAnimation

      public void addAnimation(String name, String path, int frames, int width, int height, int column, boolean useColumns)
      Adds an animation to the sprite.
      Parameters:
      name - the name of the animation
      path - the path to the animation frames
      frames - the number of frames in the animation
      width - the width of each frame
      height - the height of each frame
      column - the column of the animation frames
      useColumns - whether to use columns or rows
    • playAnimation

      public void playAnimation(String name)
      Plays the animation with the specified name.
      Parameters:
      name - the name of the animation to play
    • playAnimation

      public void playAnimation(String name, boolean once)
      Plays the animation with the specified name.
      Parameters:
      name - the name of the animation to play
      once - whether to play the animation once
    • resetAnimation

      public void resetAnimation()
      Resets the animation. The animation will start from the first frame.
    • setAnimationInterval

      public void setAnimationInterval(int interval)
      Sets the interval between animation frames.
      Parameters:
      interval - the interval between animation frames
    • getAnimationInterval

      public int getAnimationInterval()
      Gets the interval between animation frames.
      Returns:
      the interval between animation frames
    • getAnimationFrame

      public int getAnimationFrame()
      Gets the current animation frame.
      Returns:
      the current animation frame
    • setAnimationFrame

      public void setAnimationFrame(int frame)
      Sets the current animation frame.
      Parameters:
      frame - the current animation frame
    • isAnimationPlayed

      public boolean isAnimationPlayed()
      Checks if the animation is played.
      Returns:
      true if the animation is played, false otherwise
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • clone

      public AnimatedSprite clone()
      Description copied from class: Sprite
      Creates a clone of the current sprite. The cloned sprite will have the same properties as the original sprite, including its costumes, position, direction, and pen.
      Overrides:
      clone in class Sprite
      Returns:
      a new Sprite object that is a clone of the current sprite