44import org .openpatch .scratch .Sprite ;
55
66/**
7- * The AnimatedSprite class represents a sprite that can play animations. It extends the Sprite
8- * class and provides methods to add animations, play animations, set the interval between animation
7+ * The AnimatedSprite class represents a sprite that can play animations. It
8+ * extends the Sprite
9+ * class and provides methods to add animations, play animations, set the
10+ * interval between animation
911 * frames, and reset the animation.
1012 *
11- * <p>Example usage:
13+ * <p>
14+ * Example usage:
1215 *
1316 * <pre>{@code
1417 * AnimatedSprite sprite = new AnimatedSprite();
@@ -29,9 +32,10 @@ public class AnimatedSprite extends Sprite {
2932 /**
3033 * Adds an animation to the sprite.
3134 *
32- * @param name the name of the animation
33- * @param pattern the pattern (@see String#format) of the file names
34- * @param frames the number of frames in the animation
35+ * @param name the name of the animation
36+ * @param pattern the pattern (@see String#format) of the file names. The frame
37+ * starts from 1.
38+ * @param frames the number of frames in the animation
3539 */
3640 public void addAnimation (String name , String pattern , int frames ) {
3741 String [] animation = new String [frames ];
@@ -47,10 +51,29 @@ public void addAnimation(String name, String pattern, int frames) {
4751 /**
4852 * Adds an animation to the sprite.
4953 *
50- * @param name the name of the animation
51- * @param path the path to the animation frames
54+ * @param name the name of the animation
55+ * @param builder a function that builds the file names for the animation frames
56+ * @param frame the number of frames in the animation. The frame starts from
57+ * 1.
58+ */
59+ public void addAnimation (
60+ String name , java .util .function .Function <Integer , String > builder , int frame ) {
61+ String [] animation = new String [frame ];
62+ for (int i = 0 ; i < animation .length ; i ++) {
63+ String costumeName = "_animation_" + name + "_" + i ;
64+ this .addCostume (costumeName , builder .apply (i + 1 ));
65+ animation [i ] = costumeName ;
66+ }
67+ animations .put (name , animation );
68+ }
69+
70+ /**
71+ * Adds an animation to the sprite.
72+ *
73+ * @param name the name of the animation
74+ * @param path the path to the animation frames
5275 * @param frames the number of frames in the animation
53- * @param width the width of each frame
76+ * @param width the width of each frame
5477 * @param height the height of each frame
5578 */
5679 public void addAnimation (String name , String path , int frames , int width , int height ) {
@@ -60,12 +83,12 @@ public void addAnimation(String name, String path, int frames, int width, int he
6083 /**
6184 * Adds an animation to the sprite.
6285 *
63- * @param name the name of the animation
64- * @param path the path to the animation frames
86+ * @param name the name of the animation
87+ * @param path the path to the animation frames
6588 * @param frames the number of frames in the animation
66- * @param width the width of each frame
89+ * @param width the width of each frame
6790 * @param height the height of each frame
68- * @param row the row of the animation frames
91+ * @param row the row of the animation frames
6992 */
7093 public void addAnimation (String name , String path , int frames , int width , int height , int row ) {
7194 String [] animation = new String [frames ];
@@ -80,12 +103,12 @@ public void addAnimation(String name, String path, int frames, int width, int he
80103 /**
81104 * Adds an animation to the sprite.
82105 *
83- * @param name the name of the animation
84- * @param path the path to the animation frames
85- * @param frames the number of frames in the animation
86- * @param width the width of each frame
87- * @param height the height of each frame
88- * @param column the column of the animation frames
106+ * @param name the name of the animation
107+ * @param path the path to the animation frames
108+ * @param frames the number of frames in the animation
109+ * @param width the width of each frame
110+ * @param height the height of each frame
111+ * @param column the column of the animation frames
89112 * @param useColumns whether to use columns or rows
90113 */
91114 public void addAnimation (
0 commit comments