Showing code for

Toggles

On/off switches for settings like sound and music.

What zStudio needs

Set the container type to Toggle and give it exactly these two children. A toggle always starts off.

soundToggle (Toggle)
├─ onState
└─ offState

Use it

const sound = stage.get('soundToggle') as ZToggle;

// Runs when the player clicks, with the new value
sound.setCallback((isOn) => {
  setMuted(!isOn);
});

// Read the current value
if (sound.isOn()) playMusic();

// Set it from code. This also runs your callback...
sound.toggle(true);

// ...so pass false when restoring a saved setting
sound.toggle(savedSettings.sound, false);

sound.disable();
sound.enable();

// Set the same text on both states
sound.setLabelOnAllStates('labelContainer', 'Sound');