Game Actions
The game
object exposes several methods to trigger visual or audio effects: show a message, open a dialogue, play a sound, or end the game.
These methods can be called dynamically at any point during gameplay (for example inside onCollide
, onEnter
, or from custom logic).
💬 Open a dialog
To trigger a dialog manually, use game.openDialog()
:
const game = createGame({
// ...
})
game.openDialog('Hello world!')
This will display a dialog box with the provided text. You can use text effects and colors as well (see Dialogues).
Play a sound
To play a sound manually, use game.playSound()
:
game.playSound('BLIP')
game.playSound('PICKUP', 42)
game.playSound('https://pfxr/...')
game.playSound({ frequency: 300, waveForm: 1 })
You can pass a preset name, a preset + seed, a PFXR URL, or a custom sound object. See the Sounds page for more details.
✉️ Show a message
The method game.openMessage()
lets you show one or more message boxes:
game.openMessage('Hello')
game.openMessage('Hello', 'Welcome')
game.openMessage('Hello and ~welcome~')
Ask the player a question
The game.prompt()
method lets you present multiple options to the player:
await game.prompt('yes', 'no')
await game.prompt('Rock', 'Paper', 'Scissors')
This method returns a promise containing the index of the selected option: 0
→ first option, 1
→ second option…
This allows you to react based on the player’s choice:
const choice = await game.prompt('Go left', 'Go right')
if (choice === 0) {
game.openMessage('You turned left')
} else {
game.openMessage('You turned right')
}
End the game
To restart the game from the beginning, call game.end()
.
If you provide one or more strings, they will be shown in the message box before restarting.
game.end()
game.end('You win!')
game.end('Game over', 'But nice try.')
Chain actions
The methods openDialog
, openMessage
, and playSound
return a promise, which lets you wait for one to finish before continuing.
For example, wait for a message to finish before playing a sound:
await game.openMessage('Watch out...')
game.playSound('EXPLOSION')
Or create a sequence of dialogs with a sound in between:
await game.openDialog('Are you ready?')
await game.playSound('BLIP')
await game.openDialog("Let's go.")