JavaScratch Sprite API

Looks

Speech and thought bubbles, costumes, size, visibility, graphic effects and layers. Costume and backdrop numbers start at 1, like in Scratch.

void say(Object message) Sprites only

Shows a speech bubble that stays until the sprite says something else. say("") removes the bubble. Numbers are shown like Scratch shows them (10.0 is shown as 10).

Scratch blocksay [Hello!]
Parameters
messageany value: text, number, boolean, list…
Example
public void whenFlagClicked() {
    say("Hello!");
    waitSeconds(2);
    say("");          // remove the bubble
}
void say(Object message, double seconds) Sprites only

Shows a speech bubble for some seconds, then removes it. The script waits meanwhile.

Scratch blocksay [Hello!] for (2) seconds
Parameters
messagewhat to say
secondshow long to show it
Example
public void whenFlagClicked() {
    say("Hello!", 2);
    say("Welcome to JavaScratch.", 2);
}
void think(Object message) Sprites only

Shows a thought bubble (with little circles instead of a pointer).

Scratch blockthink [Hmm...]
Parameters
messagewhat to think
Example
public void whenClicked() {
    think("Hmm... what should I do?");
}
void think(Object message, double seconds) Sprites only

Shows a thought bubble for some seconds.

Scratch blockthink [Hmm...] for (2) seconds
Parameters
messagewhat to think
secondshow long to show it
Example
public void whenFlagClicked() {
    think("Zzz...", 3);
    say("I'm awake!", 1);
}
void show() Sprites only

Makes the sprite visible.

Scratch blockshow
Example
public void whenFlagClicked() {
    hide();
    waitSeconds(1);
    show();           // appears after one second
}
void hide() Sprites only

Makes the sprite invisible. Hidden sprites can't be clicked and don't touch anything.

Scratch blockhide
Example
public void whenClicked() {
    playSound("pop");
    hide();           // disappear when clicked
}
boolean isVisible() Sprites only

Tells whether the sprite is currently shown.

Returnsboolean — true if the sprite is shown
Example
public void whenKeyPressed(String key) {
    if (key.equals("h")) {
        if (isVisible()) hide(); else show();
    }
}
void switchCostumeTo(String name) Sprites only

Switches to the costume with that name (see the Costumes tab).

Scratch blockswitch costume to [costume2]
Parameters
namethe costume's name
Example
public void whenFlagClicked() {
    switchCostumeTo("bean-a");
}
void switchCostumeTo(int number) Sprites only

Switches to a costume by number. Numbers start at 1, like in Scratch, and wrap around.

Scratch blockswitch costume to (1)
Parameters
number1 for the first costume
Example
public void whenFlagClicked() {
    switchCostumeTo(pickRandom(1, 2));
}
void nextCostume() Sprites only

Switches to the next costume (back to the first after the last). Great for walking animations.

Scratch blocknext costume
Example
public void whenFlagClicked() {
    forever(() -> {
        move(3);
        nextCostume();
        waitSeconds(0.15);
        ifOnEdgeBounce();
    });
}
int getCostumeNumber() Sprites only

Reports the current costume number.

Scratch block(costume #)
Returnsint — current costume number (starting at 1)
Example
public void whenClicked() {
    say("This is costume number " + getCostumeNumber(), 1);
}
String getCostumeName() Sprites only

Reports the current costume's name.

ReturnsString — current costume name
Example
public void whenClicked() {
    nextCostume();
    say(getCostumeName(), 1);
}
void switchBackdropTo(String name) Sprites & Stage

Switches the stage's backdrop by name. Triggers whenBackdropSwitchesTo on every sprite.

Scratch blockswitch backdrop to [backdrop1]
Parameters
namea backdrop name from the stage's Backdrops tab
Example
public void whenFlagClicked() {
    switchBackdropTo("blue sky");
}
void switchBackdropTo(int number) Sprites & Stage

Switches the backdrop by number (starting at 1).

Scratch blockswitch backdrop to (1)
Parameters
number1 for the first backdrop
Example
public void whenFlagClicked() {
    switchBackdropTo(1);
}
void nextBackdrop() Sprites & Stage

Switches to the next backdrop.

Scratch blocknext backdrop
Example
public void whenKeyPressed(String key) {
    if (key.equals("n")) nextBackdrop();
}
String backdropName() Sprites & Stage

Reports the backdrop name.

Scratch block(backdrop name)
ReturnsString — the current backdrop's name
Example
public void whenFlagClicked() {
    if (backdropName().equals("night")) {
        say("It's dark!", 2);
    }
}
int backdropNumber() Sprites & Stage

Reports the backdrop number.

Scratch block(backdrop #)
Returnsint — current backdrop number (starting at 1)
Example
public void whenClicked() {
    say("Level " + backdropNumber(), 1);
}
void setEffect(String effect, double value) Sprites & Stage

Sets a graphic effect. "color" shifts the colours around the rainbow (0–200 is a full turn), "brightness" goes from -100 (black) to 100 (white), "ghost" from 0 (solid) to 100 (invisible).

Scratch blockset [ghost] effect to (50)
Parameters
effect"color", "brightness" or "ghost"
valuethe effect amount
Example
public void whenFlagClicked() {
    setEffect("ghost", 50);   // half transparent
}
void changeEffect(String effect, double by) Sprites & Stage

Changes a graphic effect by an amount.

Scratch blockchange [color] effect by (25)
Parameters
effect"color", "brightness" or "ghost"
byhow much to change it
Example
public void whenFlagClicked() {
    repeat(20, () -> changeEffect("ghost", 5));   // fade out
    hide();
    clearGraphicEffects();
}
void clearGraphicEffects() Sprites & Stage

Removes all graphic effects.

Scratch blockclear graphic effects
Example
public void whenFlagClicked() {
    clearGraphicEffects();
    show();
}
void changeSizeBy(double percent) Sprites only

Grows (positive) or shrinks (negative) the sprite.

Scratch blockchange size by (10)
Parameters
percenthow many percent to grow
Example
public void whenClicked() {
    repeat(10, () -> changeSizeBy(5));
    repeat(10, () -> changeSizeBy(-5));   // "pulse"
}
void setSizeTo(double percent) Sprites only

Sets the size in percent of the costume's normal size (100 = normal). Very small and very large sizes are limited, like in Scratch.

Scratch blockset size to (100) %
Parameters
percentnew size, e.g. 50 for half size
Example
public void whenFlagClicked() {
    setSizeTo(50);
}
double getSize() Sprites only

Reports the size.

Scratch block(size)
Returnsdouble — the size in percent
Example
public void whenFlagClicked() {
    repeatUntil(() -> getSize() >= 200, () -> changeSizeBy(2));
}
void goToFront() Sprites only

Draws this sprite in front of all other sprites.

Scratch blockgo to front
Example
public void whenClicked() {
    goToFront();
}
void goToBack() Sprites only

Draws this sprite behind all other sprites (but in front of the backdrop).

Example
public void whenFlagClicked() {
    goToBack();     // e.g. scenery sprites
}
void goBackLayers(int n) Sprites only

Moves the sprite back by some layers.

Scratch blockgo back (1) layers
Parameters
nhow many layers to go back
Example
public void whenClicked() {
    goBackLayers(1);
}
void goForwardLayers(int n) Sprites only

Moves the sprite forward by some layers.

Parameters
nhow many layers to come forward
Example
public void whenClicked() {
    goForwardLayers(1);
}