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. To add an animation with file names "walk_1.png", "walk_2.png", "walk_3.png", you can use the following code:
      sprite.addAnimation("walk", i -> "walk_" + i + ".png", 3);
      
      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. For an animation sprite sheet, where each frame is of equal width and height, you can use this method to add an animation by specifying the path to the sprite sheet, the number of frames, and the dimensions of each frame. For example, for a four-frame animation arranged in a single row, each frame being 64x64 pixels, you can add the animation using the following code:
      sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64);
      
      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. If the animation frames are arranged in rows in a sprite sheet, you can use this method to specify which row to use for the animation. For example, for a four-frame animation arranged in a single row, you want the second, each frame being 64x64 pixels, you can add the animation using the following code:
      sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64, 2);
      
      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. If the animation frames are arranged in either rows or columns in a sprite sheet, you can use this method to specify which column or row to use for the animation. Take four frame animation arranged in a single column. Each frame is 64x64 pixels. You can add the animation using the following code:
      sprite.addAnimation("walk", "assets/walk.png", 4, 64, 64, 1, true); // use column 1
      
      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