AnimatedSprite::addAnimation()
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);
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);
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);
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
Examples
import org.openpatch.scratch.extensions.animation.AnimatedSprite;
public class MySprite extends AnimatedSprite {
public MySprite() {
this.addAnimation("idle", "assets/bee_idle.png", 6, 36, 34);
}
public void run() {
this.playAnimation("idle");
}
}
import org.openpatch.scratch.Stage;
public class MyStage extends Stage {
public MyStage() {
this.add(new MySprite());
}
}
import org.openpatch.scratch.Stage;
import org.openpatch.scratch.Window;
public class MyWindow extends Window {
public MyWindow() {
Stage myStage = new MyStage();
this.setStage(myStage);
}
public static void main(String[] args) {
new MyWindow();
}
}
Syntax
Java
.addAnimation(name, pattern, frames)
.addAnimation(name, builder, frame)
.addAnimation(name, path, frames, width, height)
.addAnimation(name, path, frames, width, height, row)
.addAnimation(name, path, frames, width, height, column, useColumns)
Parameters
| Name | Data Type | Description |
|---|---|---|
| name | String | the name of the animation |
| pattern | String | the pattern (@see String#format) of the file names. The frame starts from 1. |
| frames | int | the number of frames in the animation |
| builder | Function<Integer, String> | a function that builds the file names for the animation frames |
| frame | int | the number of frames in the animation. The frame starts from 1. |
| path | String | the path to the animation frames |
| width | int | the width of each frame |
| height | int | the height of each frame |
| row | int | the row of the animation frames |
| column | int | the column of the animation frames |
| useColumns | boolean | whether to use columns or rows |
Return
void
