Skip to content

Sprite and SpriteBatch ​

Textured display objects.

ts
import { Sprite, SpriteBatch } from "willow";

Sprite ​

ts
class Sprite extends Node

Extends 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 ​

MemberDescription
readonly height: floatSource-region height in pixels.
readonly region: TextureRegionThe rectangular source region currently drawn by this sprite.
readonly texture: TextureTexture that backs the current region.
tint: ColorMultiplicative tint colour, a Color. White/opaque is Color.WHITE.
readonly width: floatSource-region width in pixels.

Sprite: methods ​

MemberDescription
setRegion(region: TextureRegion): voidReplaces the rectangular source region drawn by the sprite.
setTexture(texture: Texture): voidReplaces the sprite source with the texture's full image.
setTint(color: Color): voidEquivalent to sprite.tint = color. Multiplicative tint.

SpriteBatch ​

ts
class SpriteBatch extends Node

Extends 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 ​

MemberDescription
readonly texture: TextureTexture that backs every element of this batch.

SpriteBatch: methods ​

MemberDescription
addSprite(x: float, y: float, region?: TextureRegion): floatAdds 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): booleanRemoves an element. Returns false when the handle was already dead.
hasSprite(handle: float): booleanTrue while handle names a live element.
spriteCount(): intNumber of live elements.
clearSprites(): voidRemoves every element; all handles become dead.
setSpritePosition(handle: float, x: float, y: float): voidMoves one element (top-left, batch-local pixels).
setSpriteScale(handle: float, scaleX: float, scaleY: float): voidScales one element about its top-left corner.
setSpriteRotation(handle: float, radians: float): voidRotates one element about its top-left corner, in radians.
setSpriteAlpha(handle: float, alpha: float): voidSets one element's opacity, 0 to 1.
setSpriteTint(handle: float, color: Color): voidSets one element's tint, a Color. White/opaque is Color.WHITE.
setSpriteRegion(handle: float, region: TextureRegion): voidPoints one element at another region of the batch's texture.
setSpritePositions(positions: Float32Array | Array<float>): voidMoves 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.