text(txt?: string, opt?: TextCompOpt): TextComp

Attach and render a text to a Game Object.

paramtxt- The text to display.

paramopt- Options for the text component. See

// a simple score counter
const score = add([
    text("Score: 0"),
    pos(24, 24),
    { value: 0 },
])

player.onCollide("coin", () => {
    score.value += 1
    score.text = "Score:" + score.value
})

// with options
add([
    pos(24, 24),
    text("ohhi", {
        size: 48, // 48 pixels tall
        width: 320, // it'll wrap to next line when width exceeds this value
        font: "sans-serif", // specify any font you loaded or browser built-in
    }),
])

returnsThe text comp.

sincev2000.0

kaplay logo

Misc

Layer