Sprite and SpriteBatch
Textured display objects.
ts
import { Sprite, SpriteBatch } from "willow";Sprite
ts
class Sprite extends NodeExtends Node.
Textured sprite backed by a whole texture or a texture region.
Sprite: constructors
ts
new Sprite(path: string)
new Sprite(texture: Texture)
new Sprite(region: TextureRegion)Sprite: properties
| Member | Description |
|---|---|
readonly height: float | Source-region height in pixels. |
readonly region: TextureRegion | The rectangular source region currently drawn by this sprite. |
readonly texture: Texture | Texture that backs the current region. |
tint: Color | Multiplicative tint colour, a Color. White/opaque is Color.WHITE. |
readonly width: float | Source-region width in pixels. |
Sprite: methods
| Member | Description |
|---|---|
setRegion(region: TextureRegion): void | Replaces the rectangular source region drawn by the sprite. |
setTexture(texture: Texture): void | Replaces the sprite source with the texture's full image. |
setTint(color: Color): void | Equivalent to sprite.tint = color. Multiplicative tint. |
SpriteBatch
ts
class SpriteBatch extends NodeExtends Node.
Many simple sprites drawn as one flat batch: use SpriteBatch instead of Sprite when you have thousands of sprites that only move, scale, rotate or tint. Each element is a few floats, not a scene-graph node, so a batch holds far more sprites at speed; elements cannot take children, filters or input. All elements share the batch's one texture. Element positions are in the batch's local space and rotate about their top-left corner.
An element is named by the handle add returns (a number). A handle stays valid until its element is removed; a stale handle is rejected, never silently pointed at a different element.
Element methods carry a Sprite suffix so they never shadow the batch node's own Node members (add, remove, setPosition...).
SpriteBatch: constructors
ts
new SpriteBatch(texture: Texture)
new SpriteBatch(region: TextureRegion)A whole texture, or one region of it (elements default to that region).
SpriteBatch: properties
| Member | Description |
|---|---|
readonly texture: Texture | Texture that backs every element of this batch. |
SpriteBatch: methods
| Member | Description |
|---|---|
addSprite(x: float, y: float, region?: TextureRegion): float | Adds an element with its top-left at (x, y) and returns its handle. region picks a different region of the same texture. |
removeSprite(handle: float): boolean | Removes an element. Returns false when the handle was already dead. |
hasSprite(handle: float): boolean | True while handle names a live element. |
spriteCount(): int | Number of live elements. |
clearSprites(): void | Removes every element; all handles become dead. |
setSpritePosition(handle: float, x: float, y: float): void | Moves one element (top-left, batch-local pixels). |
setSpriteScale(handle: float, scaleX: float, scaleY: float): void | Scales one element about its top-left corner. |
setSpriteRotation(handle: float, radians: float): void | Rotates one element about its top-left corner, in radians. |
setSpriteAlpha(handle: float, alpha: float): void | Sets one element's opacity, 0 to 1. |
setSpriteTint(handle: float, color: Color): void | Sets one element's tint, a Color. White/opaque is Color.WHITE. |
setSpriteRegion(handle: float, region: TextureRegion): void | Points one element at another region of the batch's texture. |
setSpritePositions(positions: Float32Array | Array<float>): void | Moves many elements at once from a flat [x0, y0, x1, y1...] array. Pair i moves the element in slot i (the slot order addSprite handed out, with removed slots skipped); extra pairs are ignored. |