Scratch for Java

AnimatedSprite::addAnimation()

Adds an animation to the sprite.

sprite.addAnimation("walk", i -> "walk_" + i + ".png", 3);

Examples

 
import org.openpatch.scratch.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();
  }
}
 

View on GitHub

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

addAnimation()

Create Shareable URL

Select Sections