on Life Zero

Run some code when the player life count reaches zero.

info.onLifeZero(function () {})

If you’ve set a life count for your came, you can take an action when the life count reaches 0. Depending on the rules for your game, you may wish to end the game or remove a player sprite.

If you set a game life count (using setLife) and it decreases to 0 but you have no onLifeZero function in your program, the game will automatically end.

Indiviual sprites have their own lifespan. They are destroyed if their lifespan was set and then reaches 0. This is different from the life count for the game. Game lives are awarded and removed based on your own rules for gameplay.

Parameters

  • handler: the code to run when the life count reaches 0.

Example

Life zero message

Set the life count to 3. In the game update function, decrease the life count by 1 each second. Show a message when the life count becomes 0.

Single player

info.setLife(3)
game.onUpdateInterval(1000, function() {
    info.changeLifeBy(-1)
})
info.onLifeZero(function () {
    game.showLongText("Life is zero!", DialogLayout.Bottom)
})

Multiplayer

info.player2.setLife(3)
game.onUpdateInterval(1000, function() {
    info.player2.changeLifeBy(-1)
})
info.player2.onLifeZero(function () {
    game.showLongText("Life is zero!", DialogLayout.Bottom)
})

No lives, game over

Set the game life count to 9 lives. Run out the lives to end the game.

info.setLife(9)
game.onUpdateInterval(300, function() {
    info.changeLifeBy(-1)
})

See also

life, set life, change life by