Target
A target is anything that lives in the project's targets array: either the
single Stage or a Sprite. This abstract base
class covers everything the two share. You never instantiate Target directly;
you get a Stage or Sprite from a Project and use the members
below on it.
Instance fields
target.json
- Type:
object
The raw target entry from project.json. The accessors below keep it in sync;
reach into it for fields the typed API doesn't expose.
target.project
- Type:
Project
The owning project — where the asset bytes live.
Identity & audio
target.isStage
- Type:
boolean(getter)
true for the stage, false for sprites. Overridden as a constant on each
subclass, so it discriminates the union without a runtime check.
target.name
- Type:
string(get/set)
The target's name. Setting coerces to a string.
target.volume
- Type:
number(get/set) — output volume, 0–100. Defaults to100.
Costumes
target.costumes
- Type:
Costume[](getter)
The target's costumes, in order.
target.currentCostume
- Type:
number(get/set)
Index of the currently selected costume. Defaults to 0.
target.getCostume(name)
name:string- Returns:
Costume| undefined
Find a costume by name.
target.addCostume(name, data, options?)
name:string— costume name.data:Uint8Array | ArrayBuffer— image bytes (PNG/SVG/JPG/…).options.dataFormat:string(optional) — override the detected file type. When omitted it is sniffed from the bytes, falling back topng.options.rotationCenterX:number(default0) — anchor X, in costume pixels.options.rotationCenterY:number(default0) — anchor Y.options.bitmapResolution:number(default1for SVG,2otherwise) — pixels per unit.- Returns:
Costume— the new costume.
Add a costume from raw image bytes. The bytes are hashed and stored in
project.assets under their md5ext, and a costume
entry pointing at them is appended.
target.removeCostume(nameOrIndex)
nameOrIndex:string | number- Returns:
boolean—trueif a costume was removed.
Remove a costume by name or index. If the removed costume was the selected one,
currentCostume is clamped back into range. The underlying bytes are dropped only
if nothing else references them.
Sounds
target.sounds
- Type:
Sound[](getter)
The target's sounds, in order.
target.getSound(name)
name:string- Returns:
Sound| undefined
Find a sound by name.
target.addSound(name, data, options?)
name:string— sound name.data:Uint8Array | ArrayBuffer— audio bytes (WAV/MP3).options.dataFormat:string(optional) — override the detected type (sniffed, falling back towav).options.rate:number(default48000) — sample rate in Hz.options.sampleCount:number(default0) — number of samples.- Returns:
Sound— the new sound.
Add a sound from raw audio bytes, stored under its md5ext like a costume.
target.removeSound(nameOrIndex)
nameOrIndex:string | number- Returns:
boolean—trueif a sound was removed.
Variables
Variables are stored by name; the library manages the underlying ids. A variable
on the Stage is global; one on a Sprite is local.
target.variableNames
- Type:
string[](getter) — names of this target's variables.
target.getVariable(name)
name:string- Returns:
string | number | boolean | undefined
Read a variable's value by name.
target.setVariable(name, value)
name:stringvalue:string | number | boolean- Returns:
string— the variable's id.
Create or update a variable by name (an upsert). Returns the id, which you rarely need.
target.deleteVariable(name)
name:string- Returns:
boolean—trueif a variable was deleted.
Lists
target.listNames
- Type:
string[](getter) — names of this target's lists.
target.getList(name)
name:string- Returns:
Array<string | number> | undefined
Read a list's contents by name.
target.setList(name, items?)
name:stringitems:Array<string | number>(default[])- Returns:
string— the list's id.
Create or replace a list by name.
target.deleteList(name)
name:string- Returns:
boolean—trueif a list was deleted.
Blocks
target.blocks
- Type:
object(getter)
The raw blocks object for advanced scripting edits. Keys are block ids; values
are block definitions. Mutate it directly for low-level script changes — generate
fresh ids with uid(). See
Authoring scripts.
Subclasses
Target is never used directly. See: