Sprite
A character on the stage: it has a position, a direction, costumes, and the motion, looks, sound, and sensing abilities that game scripts control. Most of the code learners write lives on a sprite.
The Sprite engine class. Most members are available as free functions; this page documents the underlying API for advanced use.
Sprite.Edge: Readonly<{ BOTTOM: symbol; LEFT: symbol; RIGHT: symbol; TOP: symbol; }>The names of the stage’s edges (top, bottom, left, and right).
Since 1.0.0
RotationStyle
Section titled “RotationStyle”Sprite.RotationStyle: Readonly<{ ALL_AROUND: symbol; LEFT_RIGHT: symbol; DONT_ROTATE: symbol; }>The available rotation styles a sprite can use (all around, left-right, or don’t rotate).
Since 1.0.0
andClones
Section titled “andClones”Sprite.andClones(): this[]Returns this sprite together with all of its clones as one list.
Since 1.0.0
answer
Section titled “answer”Sprite.answer: string | nullThe most recent answer the player typed in response to a question.
Since 1.0.0
arrayIncludes
Section titled “arrayIncludes”Sprite.arrayIncludes(array: T[], value: T): booleanChecks whether a list contains a given value.
Since 1.0.0
askAndWait
Section titled “askAndWait”Sprite.askAndWait(question: string): Yielding<void>Asks the player a question and pauses until they answer, hiding any speech bubble first.
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”Sprite.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”Sprite.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”Sprite.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”Sprite.clearPen(): voidErases everything that has been drawn on the stage with the pen.
Since 1.0.0
clones
Section titled “clones”Sprite.clones: this[]The list of clones made from this sprite.
Since 1.0.0
colorTouching
Section titled “colorTouching”Sprite.colorTouching(color: Color | string, target: Sprite | Stage): booleanChecks whether a color on this sprite is touching a color or another sprite.
Since 1.0.0
compare
Section titled “compare”Sprite.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”Sprite.costume: CostumeThe Costume object currently being shown.
Since 1.0.0
costumeNumber
Section titled “costumeNumber”Sprite.costumeNumber: numberThe number of the costume currently being shown (1 is the first costume).
Since 1.0.0
createClone
Section titled “createClone”Sprite.createClone(data?: unknown): voidMakes a copy of this sprite that starts running its “when I start as a clone” scripts.
Since 1.0.0
degToRad
Section titled “degToRad”Sprite.degToRad(deg: number): numberConverts an angle in degrees to radians.
Since 1.0.0
degToScratch
Section titled “degToScratch”Sprite.degToScratch(deg: number): numberConverts a standard angle in degrees to Scratch’s direction system.
Since 1.0.0
deleteThisClone
Section titled “deleteThisClone”Sprite.deleteThisClone(): voidDeletes this clone and stops its scripts (does nothing on the original sprite).
Since 1.0.0
direction
Section titled “direction”Sprite.direction: numberThe direction the sprite is pointing, in degrees (90 is to the right).
Since 1.0.0
effects
Section titled “effects”Sprite.effects: _EffectMapThe graphic effects (like color, ghost, or fisheye) currently applied to this sprite or stage.
Since 1.0.0
getSound
Section titled “getSound”Sprite.getSound(soundName: string): Sound | undefinedFinds one of this sprite’s sounds by its name or number.
Since 1.0.0
Sprite.glide(seconds: number, x: number, y: number): Yielding<void>Smoothly slides the sprite to a new position over 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
Example
yield* this.glide(1, 100, 50)Sprite.goto(x: number, y: number): voidMoves the sprite instantly to the given x and y position, drawing a pen line if the pen is down.
Since 1.0.0
Example
this.goto(0, 0)Sprite.id: UIDA unique identifier for this sprite or stage.
Since 1.0.0
ifOnEdgeBounce
Section titled “ifOnEdgeBounce”Sprite.ifOnEdgeBounce(): voidMakes the sprite turn around when it reaches the edge of the stage.
Since 1.0.0
indexInArray
Section titled “indexInArray”Sprite.indexInArray(array: T[], value: T): numberFinds the position of a value within a list.
Since 1.0.0
itemOf
Section titled “itemOf”Sprite.itemOf(array: T[], index: number): T | ""Gets an item from a list by its position.
Since 1.0.0
keyPressed
Section titled “keyPressed”Sprite.keyPressed(name: string): booleanChecks whether the given key is currently being pressed.
Since 1.0.0
letterOf
Section titled “letterOf”Sprite.letterOf(string: string, index: number): stringGets a single letter from text by its position.
Since 1.0.0
loudness
Section titled “loudness”Sprite.loudness: numberHow loud the microphone input is, from 0 to 100.
Since 1.0.0
Sprite.mouse: MouseThe mouse, used to read the pointer’s position and whether it’s pressed.
Since 1.0.0
Sprite.move(dist: number): voidMoves the sprite forward in the direction it is facing by the given number of steps.
Since 1.0.0
Example
this.move(10)moveAhead
Section titled “moveAhead”Sprite.moveAhead(value?: number | Sprite): voidMoves the sprite forward in the layers, either by a number of layers or in front of another sprite.
Since 1.0.0
moveBehind
Section titled “moveBehind”Sprite.moveBehind(value?: number | Sprite): voidMoves the sprite backward in the layers, either by a number of layers or behind another sprite.
Since 1.0.0
nearestEdge
Section titled “nearestEdge”Sprite.nearestEdge(): symbol | nullFinds which edge of the stage the sprite is touching, or null if it isn’t touching one.
Since 1.0.0
normalizeDeg
Section titled “normalizeDeg”Sprite.normalizeDeg(deg: number): numberWraps an angle so it stays within the range of -180 to 180 degrees.
Since 1.0.0
penColor
Section titled “penColor”Sprite.penColor: ColorThe color the pen draws with.
Since 1.0.0
penDown
Section titled “penDown”Sprite.penDown: booleanWhether the pen is down, so the sprite draws a line as it moves.
Since 1.0.0
penSize
Section titled “penSize”Sprite.penSize: numberThe thickness of the line the pen draws.
Since 1.0.0
playSoundUntilDone
Section titled “playSoundUntilDone”Sprite.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")positionInFence
Section titled “positionInFence”Sprite.positionInFence(): voidNudges the sprite back so it stays at least partly inside the stage’s edges.
Since 1.0.0
radToDeg
Section titled “radToDeg”Sprite.radToDeg(rad: number): numberConverts an angle in radians to degrees.
Since 1.0.0
radToScratch
Section titled “radToScratch”Sprite.radToScratch(rad: number): numberConverts an angle in radians to a Scratch direction.
Since 1.0.0
random
Section titled “random”Sprite.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”Sprite.restartTimer(): voidResets the timer back to zero.
Since 1.0.0
rotationStyle
Section titled “rotationStyle”Sprite.rotationStyle: RotationStyleHow the sprite turns when its direction changes (all around, left-right, or not at all).
Since 1.0.0
Sprite.say(text: string): voidShows a speech bubble with the given text above the sprite.
Since 1.0.0
Example
this.say("Hello!")sayAndWait
Section titled “sayAndWait”Sprite.sayAndWait(text: string, seconds: number): Yielding<void>Shows a speech bubble for the given number of seconds, pausing the script until it’s done.
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
scratchTan
Section titled “scratchTan”Sprite.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”Sprite.scratchToDeg(scratchDir: number): numberConverts a Scratch direction to a standard angle in degrees.
Since 1.0.0
scratchToRad
Section titled “scratchToRad”Sprite.scratchToRad(scratchDir: number): numberConverts a Scratch direction to an angle in radians.
Since 1.0.0
Sprite.size: numberThe sprite’s size as a percentage, where 100 is its normal size.
Since 1.0.0
sprites
Section titled “sprites”Sprite.sprites: Partial<Record<string, Sprite>>All of the sprites in the project, keyed by name.
Since 1.0.0
Sprite.stage: StageThe stage of the project, which all sprites share.
Since 1.0.0
Sprite.stamp(): voidStamps a copy of the sprite’s current look onto the stage with the pen.
Since 1.0.0
startSound
Section titled “startSound”Sprite.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
Sprite.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”Sprite.stopAllOfMySounds(): voidStops only the sounds that this sprite or stage is playing.
Since 1.0.0
stopAllSounds
Section titled “stopAllSounds”Sprite.stopAllSounds(): voidStops every sound playing in the whole project.
Since 1.0.0
stringIncludes
Section titled “stringIncludes”Sprite.stringIncludes(string: string, substring: string): booleanChecks whether one piece of text contains another, ignoring upper/lower case.
Since 1.0.0
Sprite.think(text: string): voidShows a thought bubble with the given text above the sprite.
Since 1.0.0
thinkAndWait
Section titled “thinkAndWait”Sprite.thinkAndWait(text: string, seconds: number): Yielding<void>Shows a thought bubble for the given number of seconds, pausing the script until it’s done.
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
Sprite.timer: numberThe number of seconds since the timer was last started or reset.
Since 1.0.0
toBoolean
Section titled “toBoolean”Sprite.toBoolean(value: unknown): booleanConverts a value into true or false, following Scratch’s rules.
Since 1.0.0
toNumber
Section titled “toNumber”Sprite.toNumber(value: unknown): numberConverts a value into a number, treating invalid values as 0.
Since 1.0.0
toString
Section titled “toString”Sprite.toString(value: unknown): stringConverts a value into text.
Since 1.0.0
touching
Section titled “touching”Sprite.touching(target: "mouse" | "edge" | Sprite | Stage, fast?: boolean): booleanChecks whether the sprite is touching the mouse pointer, the edge, or another sprite.
Since 1.0.0
triggers
Section titled “triggers”Sprite.triggers: Trigger[]The list of event triggers (like “when green flag clicked”) attached to this sprite or stage.
Since 1.0.0
Sprite.vars: objectThe variables belonging to this sprite or stage.
Since 1.0.0
visible
Section titled “visible”Sprite.visible: booleanWhether the sprite is currently shown on the stage.
Since 1.0.0
Sprite.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
Sprite.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”Sprite.watchers: Partial<Record<string, Watcher>>The variable watchers shown on the stage for this sprite or stage, keyed by name.
Since 1.0.0
wrapClamp
Section titled “wrapClamp”Sprite.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
Sprite.x: numberThe sprite’s x position (left-right) on the stage.
Since 1.0.0
Sprite.y: numberThe sprite’s y position (up-down) on the stage.
Since 1.0.0