Skip to content

Trigger

One “when … happens, do this” rule: it pairs an event type (green flag, a key press, a broadcast, a click, and so on) with the script that should run when that event fires. A sprite’s triggers are how its scripts get started.

The Trigger engine class. Most members are available as free functions; this page documents the underlying API for advanced use.

Trigger.BACKDROP_CHANGED: typeof Trigger.BACKDROP_CHANGED

Trigger type: fires when the stage’s backdrop changes.

Since 1.0.0

Trigger.BROADCAST: typeof Trigger.BROADCAST

Trigger type: fires when a matching broadcast message is received.

Since 1.0.0

Trigger.CLICKED: typeof Trigger.CLICKED

Trigger type: fires when the sprite or stage is clicked with the mouse.

Since 1.0.0

Trigger.CLICKED_OR_TAPPED: typeof Trigger.CLICKED_OR_TAPPED

Fires for both mouse clicks and touch taps on a sprite/stage. The same physical interaction (a tap on a touch device that the browser then synthesizes a click for) only fires this trigger once.

Since 1.0.0

Trigger.CLONE_START: typeof Trigger.CLONE_START

Trigger type: fires when a clone of the sprite is first created.

Since 1.0.0

Trigger.GREEN_FLAG: typeof Trigger.GREEN_FLAG

Trigger type: fires when the green flag is clicked to start the project.

Since 1.0.0

Trigger.KEY_PRESSED: typeof Trigger.KEY_PRESSED

Trigger type: fires when a specific key on the keyboard is pressed.

Since 1.0.0

Trigger.LOUDNESS_GREATER_THAN: typeof Trigger.LOUDNESS_GREATER_THAN

Trigger type: fires when the microphone loudness rises above a value.

Since 1.0.0

Trigger.TIMER_GREATER_THAN: typeof Trigger.TIMER_GREATER_THAN

Trigger type: fires when the timer rises above a value.

Since 1.0.0

Trigger.TOUCH_END: typeof Trigger.TOUCH_END

Trigger type: fires when a finger lifts off the sprite or stage.

Since 1.0.0

Trigger.TOUCH_MOVE: typeof Trigger.TOUCH_MOVE

Trigger type: fires when a finger slides across the sprite or stage.

Since 1.0.0

Trigger.TOUCH_START: typeof Trigger.TOUCH_START

Trigger type: fires when a finger first touches the sprite or stage.

Since 1.0.0

Trigger.clone(): Trigger

Makes a copy of this trigger with the same type, options, and script.

Since 1.0.0

Trigger.done: boolean

Whether this trigger’s script has finished running.

Since 1.0.0

Trigger.getGenerator(target: Sprite | Stage, args?: unknown[]): Generator

Creates a fresh generator for this trigger’s script on a sprite so its steps can be advanced one at a time.

Since 1.0.0

Trigger.isEdgeActivated: boolean

Whether this trigger only fires on the moment a condition first becomes true (like the timer or loudness rising past a value), not continuously.

Since 1.0.0

Trigger.matches(trigger: Trigger["trigger"], options: Trigger["options"] | undefined, target: Sprite | Stage): boolean

Checks whether this trigger should fire for a given event type and options on a particular sprite or stage.

Since 1.0.0

Trigger.option(option: string, target: Sprite | Stage): number | string | boolean | undefined

Looks up one of this trigger’s options for a sprite, running it as a function first if it was set up as one.

Since 1.0.0

Trigger.start(target: Sprite | Stage): Promise<void>

Starts running this trigger’s script on a sprite, returning a promise that resolves when the script finishes or is stopped.

Since 1.0.0

Example

await myTrigger.start(this.sprite)
Trigger.step(): void

Runs the script forward by one step, marking it done when it reaches the end.

Since 1.0.0

Trigger.trigger: symbol

The trigger-type symbol that says what kind of event starts this script.

Since 1.0.0