Skip to content

Commit 6bf1f61

Browse files
committed
add builder for addAnimation
1 parent 74638a9 commit 6bf1f61

5 files changed

Lines changed: 69 additions & 20 deletions

File tree

CHANGELOG.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,20 @@ index: 4
44
lang: en
55
---
66

7+
## 4.21.0
8+
9+
- 🚀 Feat: Add `addAnimation` with a builder function to the AnimatedSprite
10+
class. This allows you to add animations with a more flexible API. The function
11+
receives the frame number and returns the path to the image for that frame.
12+
This is useful if you want to generate the image path dynamically or if you
13+
want to use a naming convention for your images. To be consistant with the
14+
pattern the frames start counting at 1.
15+
16+
```java
17+
mySprite.addAnimation("run", frame -> "test" + frame + ".png", 10);
18+
```
19+
20+
721
## 4.20.0
822

923
- 🚀 Feat: Add 9-Slice scaling to the Sprite class. This allows you to scale a sprite in a way that the corners are not stretched, but the middle part is stretched. This is useful for UI elements like buttons or panels.

docs/de/book/reference/extensions/animation/animated-sprite/looks/addAnimation.md.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"description": "Fügt eine Animation hinzu. Die Animation kann entweder eine Reihe von einzelnen Bildern oder ein Teil eines Spritesheets sein.",
88
"syntax": [
99
".addAnimation(name, pattern, frames)",
10+
".addAnimation(name, builder, frames)",
1011
".addAnimation(name, path, frames, width, height)",
1112
".addAnimation(name, path, frames, width, height, row)"
1213
],
@@ -42,6 +43,11 @@
4243
"description": "Ein Muster für das Finden von Bilden, welche die einzelnen Frames der Animation repräsentieren.",
4344
"type": "String"
4445
},
46+
{
47+
"name": "builder",
48+
"description": "Eine Funktion, die einen String für jedes Frame der Animation zurückgibt. Diese Funktion wird aufgerufen, um die Bilder zu laden.",
49+
"type": "Function<Integer, String>"
50+
},
4551
{
4652
"name": "frames",
4753
"description": "Die Anzahl der Frames der Animation.",

docs/en/book/reference/extensions/animation/animated-sprite/looks/addAnimation.md.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"description": "Add an animation to the sprite. The animation can either be multiple images or parts of a spritesheet.",
88
"syntax": [
99
".addAnimation(name, pattern, frames)",
10+
".addAnimation(name, builder, frames)",
1011
".addAnimation(name, path, frames, width, height)",
1112
".addAnimation(name, path, frames, width, height, row)"
1213
],
@@ -42,6 +43,11 @@
4243
"description": "A pattern for finding images representing the frames.",
4344
"type": "String"
4445
},
46+
{
47+
"name": "builder",
48+
"description": "A function that returns a String for each frame of the animation. This function is called to load the images.",
49+
"type": "Function<Integer, String>"
50+
},
4551
{
4652
"name": "frames",
4753
"description": "The number of frames for this animation.",

resources/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ author.url=https://openpatch.org
4040
# This is NOT a direct link to where to download it.
4141

4242
library.url=https://github.com/openpatch/scratch-for-java
43-
library.version=4.20.0
43+
library.version=4.21.0
4444

4545

4646
# Set the category (or categories) of your Library from the following list:

src/org/openpatch/scratch/extensions/animation/AnimatedSprite.java

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@
44
import 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

Comments
 (0)