JavaScratch Sprite API

Control

Waiting, loops, stopping and clones. Loop helpers wait one frame (1/30 s) after each repetition, just like Scratch's loops, so animations are visible.

void waitSeconds(double seconds) Sprites & Stage

Pauses this script for some seconds. Other scripts keep running.

Scratch blockwait (1) seconds
Parameters
secondshow long to wait (decimals allowed)

Note: Named waitSeconds because wait() already exists in every Java object.

Example
public void whenFlagClicked() {
    say("3"); waitSeconds(1);
    say("2"); waitSeconds(1);
    say("1"); waitSeconds(1);
    say("Go!", 1);
}
void forever(Runnable body) Sprites & Stage

Repeats the body forever, waiting one frame (1/30 s) after each repetition. Ends only when the project is stopped (or with stopThisScript()).

Scratch blockforever { }
Parameters
bodya lambda: () -> { ... }
Example
public void whenFlagClicked() {
    forever(() -> {
        turnRight(3);
        move(2);
    });
}
void repeat(int times, Runnable body) Sprites & Stage

Repeats the body a number of times, one repetition per frame.

Scratch blockrepeat (10) { }
Parameters
timeshow many times
bodya lambda: () -> { ... }
Example
public void whenFlagClicked() {
    repeat(36, () -> {
        turnRight(10);    // one full turn in 36 frames
    });
}
void repeatUntil(BooleanSupplier condition, Runnable body) Sprites & Stage

Repeats the body until the condition becomes true.

Scratch blockrepeat until <> { }
Parameters
conditiona lambda returning boolean: () -> ...
bodya lambda: () -> { ... }
Example
public void whenFlagClicked() {
    goTo(0, 0);
    repeatUntil(() -> isTouching("edge"), () -> move(5));
    say("Ouch!", 1);
}
void waitUntil(BooleanSupplier condition) Sprites & Stage

Waits until the condition becomes true.

Scratch blockwait until <>
Parameters
conditiona lambda returning boolean
Example
public void whenFlagClicked() {
    say("Click the mouse to start");
    waitUntil(() -> isMouseDown());
    say("Go!", 1);
}
void nextFrame() Sprites & Stage

Waits until the next screen refresh (1/30 s). Call it inside your own while / for loops so the animation is visible and the stop sign works.

Note: A loop that never calls any JavaScratch method can't be stopped by the stop sign.

Example
public void whenFlagClicked() {
    for (int i = 0; i < 100; i++) {
        move(2);
        nextFrame();          // without this the loop would finish instantly
    }
    while (!isTouching("edge")) {
        changeYBy(3);
        nextFrame();
    }
}
if / else Sprites & Stage

Plain Java if-statements work like Scratch's if and if-else blocks. Compare numbers with < > == and text with .equals(…).

Scratch blockif <> then / else
Example
public void whenFlagClicked() {
    forever(() -> {
        if (isTouching("mouse-pointer")) {
            setEffect("brightness", 30);
        } else {
            setEffect("brightness", 0);
        }
    });
}
void stopAll() Sprites & Stage

Stops every script of every sprite, like the red stop sign.

Scratch blockstop [all]
Example
public void whenFlagClicked() {
    waitUntil(() -> isTouching("edge"));
    say("Game over");
    stopAll();
}
void stopThisScript() Sprites & Stage

Stops the method (script) that is running this line.

Scratch blockstop [this script]
Example
public void whenFlagClicked() {
    forever(() -> {
        move(5);
        if (getX() > 200) {
            stopThisScript();    // leaves the forever loop
        }
    });
}
void stopOtherScriptsInSprite() Sprites & Stage

Stops all other running methods of this sprite.

Scratch blockstop [other scripts in sprite]
Example
public void whenClicked() {
    stopOtherScriptsInSprite();   // stop walking
    say("You caught me!", 2);
}
public void whenIStartAsClone() Sprites only

Event method: runs in a new clone right after createClone(). Use it to give every clone its own behaviour.

Scratch blockwhen I start as a clone
Example
public void whenFlagClicked() {
    hide();
    repeat(10, () -> createClone());
}

public void whenIStartAsClone() {
    goTo("random position");
    show();
    forever(() -> turnRight(5));
}
void createClone() Sprites only

Creates a copy of this sprite (position, costume, size, effects and field values are copied). The clone then runs its whenIStartAsClone methods. At most 300 clones exist at the same time.

Scratch blockcreate clone of [myself]
Example
public void whenFlagClicked() {
    hide();
    forever(() -> {
        createClone();
        waitSeconds(0.5);
    });
}

public void whenIStartAsClone() {
    goTo(pickRandom(-220, 220), 180);
    show();
    repeatUntil(() -> getY() < -170, () -> changeYBy(-5));
    deleteThisClone();
}
void createCloneOf(String spriteName) Sprites & Stage

Creates a clone of another sprite (or of this sprite with "myself"). Available on the stage too.

Scratch blockcreate clone of [Sprite1]
Parameters
spriteNamethe sprite's name, or "myself"
Example
public void whenKeyPressed(String key) {
    if (key.equals("space")) createCloneOf("Ball");
}
void deleteThisClone() Sprites only

Deletes this clone and stops its scripts. Does nothing in the original sprite.

Scratch blockdelete this clone
Example
public void whenIStartAsClone() {
    show();
    waitSeconds(3);
    deleteThisClone();
}
boolean isClone() Sprites only

Tells whether this is a clone.

Returnsboolean — true for clones, false for the original
Example
public void whenClicked() {
    if (isClone()) {
        deleteThisClone();
    } else {
        say("I'm the original!", 1);
    }
}