health(hp: number, maxHP?: number): HealthComp

Handles health related logic and events.

paramhp- The initial health points.

parammaxHP- The maximum health points.

const player = add([
    health(3),
])

player.onCollide("bad", (bad) => {
    player.hurt(1)
    bad.hurt(1)
})

player.onCollide("apple", () => {
    player.heal(1)
})

player.on("hurt", () => {
    play("ouch")
})

// triggers when hp reaches 0
player.on("death", () => {
    destroy(player)
    go("lose")
})

returnsThe health comp.

sincev2000.0

kaplay logo

Misc

Layer