arcade2d
Class

LayerSet

graphics/layer-set.ts:45

An ordered, named set of render layers — the developer-defined draw order a game stamps onto its graphics.

The engine ships no built-in layers. A game declares its own bands once, back-to-front, with defineLayers, and passes the resulting Layer tokens to its graphics components via GraphicsOptions.layer. A Scene created with the set renders each band in order, and within a band keeps insertion (spawn) order — so the cross-band order is stable no matter when objects spawn, while same-band order still falls out of creation order as before.

Type-safe lookup

Like AssetBundle, a LayerSet is just data with compile-checked keys. Because defineLayers captures the names as their literal types, LayerSet.get only accepts a name the set actually declares — a typo is a tsc error, not a runtime ErrorCode.LAYER_NOT_IN_SET that fires only when the mis-named graphic first spawns.

Construct via defineLayers, not new — the factory captures the literal name union the constructor's generic alone cannot.

Example

export const layers = defineLayers('ground', 'structures', 'characters', 'ui');

const world = game.createWorld({ layers });

// ground (back) < structures < characters < ui (front)
new Sprite(object, texture, { layer: layers.get('characters') });
layers.get('charcters'); // tsc error: not assignable to the name union

Accessors

readonly #
layers: readonly Layer[]

The Layer tokens, in back-to-front order. The Scene reads this to build one render container per layer.

readonly #
names: readonly string[]

The declared layer names, in back-to-front order.

Methods

#
get(name: T[number]): Layer

Resolves a layer name to its Layer token. The argument is checked against the set's declared names at compile time.

Parameters

  • name T[number]

Returns

Layer

The matching Layer.

Throws

EngineError with code ErrorCode.LAYER_NOT_IN_SET when the name isn't in the set. With a statically-typed set this is unreachable from well-typed code; it guards dynamic callers.

#
has(name: string): boolean

Whether the set declares a layer with the given name.

Parameters

  • name string

Returns

boolean
#
owns(layer: Layer): boolean

Whether layer was minted by this set. Used by Scene to reject a token from a different LayerSet.

Parameters

Returns

boolean
ESC