Costumes & sounds
Costumes (images) and sounds (audio) are the two kinds of media a target owns. They behave almost identically, so most of what follows applies to both.
Listing and finding
Adding media from bytes
Pass a name and the raw bytes. The file type is sniffed from the bytes, the asset is stored under its MD5, and a new entry pointing at it is appended:
Costume options
addCostume accepts an options object:
The rotation center is the point a sprite rotates and is positioned around. For a centred costume, set it to half the image's width and height.
Sound options
addSound takes dataFormat, rate (sample rate in Hz,
default 48000) and sampleCount (default 0).
Swapping a costume's image
Assign new bytes to a costume's data to replace the
picture while keeping the costume's name and place in the list. The asset is
automatically re-hashed:
data keeps the costume's existing dataFormat. To swap a PNG for an SVG,
remove the old costume and addCostume the new one instead, so the file
extension stays correct.
Exporting media
data is the raw Uint8Array, so writing a costume or sound to disk is just:
Sharing assets between targets
Because assets are keyed by content hash, two targets that hold bytes with the same MD5 transparently share one file in the zip. A common pattern is to copy one costume's bytes onto another sprite:
When you later remove one of them, the shared bytes survive until the last reference is gone.
Removing media
Removing a costume that was the selected one clamps currentCostume back into
range. The underlying bytes are dropped only if nothing else references them.
Costume vs. sound properties
See the Costume / Sound reference for full details.