Stage
The play area and backdrop behind the sprites. It behaves like a special sprite that cannot move or be cloned, but holds the backdrops and can run its own stage-wide scripts (for example, when the backdrop changes).
The Stage engine class. Most members are available as free functions; this page documents the underlying API for advanced use.
answer
Section titled “answer”Stage.answer: string | nullThe most recent answer the player typed in response to a question.
Since 1.0.0
arrayIncludes
Section titled “arrayIncludes”Stage.arrayIncludes(array: T[], value: T): booleanChecks whether a list contains a given value.
Since 1.0.0
askAndWait
Section titled “askAndWait”Stage.askAndWait(question: string): Yielding<void>Asks the player a question and pauses the script until they type an answer.
In engine code this generator is awaited with yield*; in sprite code the free-function wrapper handles it.
Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.
Since 1.0.0
audioEffects
Section titled “audioEffects”Stage.audioEffects: AudioEffectMapThe sound effects (like pitch or pan) currently applied to this sprite or stage’s sounds.
Since 1.0.0
broadcast
Section titled “broadcast”Stage.broadcast(name: string): Promise<void>Sends a broadcast message and continues without waiting for the receiving scripts to finish.
Since 1.0.0
broadcastAndWait
Section titled “broadcastAndWait”Stage.broadcastAndWait(name: string): Yielding<void>Sends a broadcast message and pauses the script until every receiving script has finished.
In engine code this generator is awaited with yield*; in sprite code the free-function wrapper handles it.
Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.
Since 1.0.0
clearPen
Section titled “clearPen”Stage.clearPen(): voidErases everything that has been drawn on the stage with the pen.
Since 1.0.0
compare
Section titled “compare”Stage.compare(v1: unknown, v2: unknown): numberCompares two values the way Scratch does, returning a negative, zero, or positive number.
Since 1.0.0
costume
Section titled “costume”Stage.costume: CostumeThe Costume object currently being shown.
Since 1.0.0
costumeNumber
Section titled “costumeNumber”Stage.costumeNumber: numberThe number of the backdrop currently being shown (1 is the first backdrop).
Since 1.0.0
degToRad
Section titled “degToRad”Stage.degToRad(deg: number): numberConverts an angle in degrees to radians.
Since 1.0.0
degToScratch
Section titled “degToScratch”Stage.degToScratch(deg: number): numberConverts a standard angle in degrees to Scratch’s direction system.
Since 1.0.0
effects
Section titled “effects”Stage.effects: _EffectMapThe graphic effects (like color, ghost, or fisheye) currently applied to this sprite or stage.
Since 1.0.0
Stage.fence: { left: number right: number top: number bottom: number }The boundaries of the stage that keep sprites from wandering too far off screen.
Since 1.0.0
fireBackdropChanged
Section titled “fireBackdropChanged”Stage.fireBackdropChanged(): Promise<void>Fires the “when backdrop switches to” triggers for the current backdrop.
Since 1.0.0
getSound
Section titled “getSound”Stage.getSound(soundName: string): Sound | undefinedFinds one of this sprite’s sounds by its name or number.
Since 1.0.0
height
Section titled “height”Stage.height: numberThe height of the stage in pixels.
Since 1.0.0
Stage.id: UIDA unique identifier for this sprite or stage.
Since 1.0.0
indexInArray
Section titled “indexInArray”Stage.indexInArray(array: T[], value: T): numberFinds the position of a value within a list.
Since 1.0.0
itemOf
Section titled “itemOf”Stage.itemOf(array: T[], index: number): T | ""Gets an item from a list by its position.
Since 1.0.0
keyPressed
Section titled “keyPressed”Stage.keyPressed(name: string): booleanChecks whether the given key is currently being pressed.
Since 1.0.0
letterOf
Section titled “letterOf”Stage.letterOf(string: string, index: number): stringGets a single letter from text by its position.
Since 1.0.0
loudness
Section titled “loudness”Stage.loudness: numberHow loud the microphone input is, from 0 to 100.
Since 1.0.0
Stage.mouse: MouseThe mouse, used to read the pointer’s position and whether it’s pressed.
Since 1.0.0
normalizeDeg
Section titled “normalizeDeg”Stage.normalizeDeg(deg: number): numberWraps an angle so it stays within the range of -180 to 180 degrees.
Since 1.0.0
playSoundUntilDone
Section titled “playSoundUntilDone”Stage.playSoundUntilDone(soundName: string): Yielding<void>Plays a sound and pauses the script until that sound finishes.
In engine code this generator is awaited with yield*; in sprite code the free-function wrapper handles it.
Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.
Since 1.0.0
Example
yield* this.playSoundUntilDone("Meow")radToDeg
Section titled “radToDeg”Stage.radToDeg(rad: number): numberConverts an angle in radians to degrees.
Since 1.0.0
radToScratch
Section titled “radToScratch”Stage.radToScratch(rad: number): numberConverts an angle in radians to a Scratch direction.
Since 1.0.0
random
Section titled “random”Stage.random(a: number, b: number): numberPicks a random number between two values (whole numbers if both ends are whole).
Since 1.0.0
restartTimer
Section titled “restartTimer”Stage.restartTimer(): voidResets the timer back to zero.
Since 1.0.0
scratchTan
Section titled “scratchTan”Stage.scratchTan(angle: number): numberCalculates the tangent of an angle in degrees, matching Scratch’s behavior at special angles.
Since 1.0.0
scratchToDeg
Section titled “scratchToDeg”Stage.scratchToDeg(scratchDir: number): numberConverts a Scratch direction to a standard angle in degrees.
Since 1.0.0
scratchToRad
Section titled “scratchToRad”Stage.scratchToRad(scratchDir: number): numberConverts a Scratch direction to an angle in radians.
Since 1.0.0
sprites
Section titled “sprites”Stage.sprites: Partial<Record<string, Sprite>>All of the sprites in the project, keyed by name.
Since 1.0.0
Stage.stage: StageThe stage of the project, which all sprites share.
Since 1.0.0
startSound
Section titled “startSound”Stage.startSound(soundName: string): Yielding<void>Starts playing a sound and continues the script right away without waiting for it to finish.
In engine code this generator is awaited with yield*; in sprite code the free-function wrapper handles it.
Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.
Since 1.0.0
Stage.stop(option: StopOption): voidStops running scripts based on the given option (for example, all scripts or just this one).
Since 1.0.0
stopAllOfMySounds
Section titled “stopAllOfMySounds”Stage.stopAllOfMySounds(): voidStops only the sounds that this sprite or stage is playing.
Since 1.0.0
stopAllSounds
Section titled “stopAllSounds”Stage.stopAllSounds(): voidStops every sound playing in the whole project.
Since 1.0.0
stringIncludes
Section titled “stringIncludes”Stage.stringIncludes(string: string, substring: string): booleanChecks whether one piece of text contains another, ignoring upper/lower case.
Since 1.0.0
Stage.timer: numberThe number of seconds since the timer was last started or reset.
Since 1.0.0
toBoolean
Section titled “toBoolean”Stage.toBoolean(value: unknown): booleanConverts a value into true or false, following Scratch’s rules.
Since 1.0.0
toNumber
Section titled “toNumber”Stage.toNumber(value: unknown): numberConverts a value into a number, treating invalid values as 0.
Since 1.0.0
toString
Section titled “toString”Stage.toString(value: unknown): stringConverts a value into text.
Since 1.0.0
triggers
Section titled “triggers”Stage.triggers: Trigger[]The list of event triggers (like “when green flag clicked”) attached to this sprite or stage.
Since 1.0.0
Stage.vars: objectThe variables belonging to this sprite or stage.
Since 1.0.0
Stage.wait(secs: number): Yielding<void>Pauses the running script for the given number of seconds.
In engine code this generator is awaited with yield*; in sprite code the free-function wrapper handles it.
Pauses the script (a generator). In sprite code the free-function wrapper awaits it for you.
Since 1.0.0
Stage.warp(procedure: GeneratorFunction): (...args: unknown[]) => voidRuns a script all at once without pausing, so it finishes in a single frame.
Since 1.0.0
watchers
Section titled “watchers”Stage.watchers: Partial<Record<string, Watcher>>The variable watchers shown on the stage for this sprite or stage, keyed by name.
Since 1.0.0
Stage.width: numberThe width of the stage in pixels.
Since 1.0.0
wrapClamp
Section titled “wrapClamp”Stage.wrapClamp(n: number, min: number, max: number): numberKeeps a number within a range, wrapping any extra around to the other end.
Since 1.0.0