tip: make a toggle button

To make a button that stops or starts a music loop (variable name: "music") depending on whether the music is playing already, create a true/false variable (in this case, "musicplaying") and change it with the button:

on (release) {
if (musicplaying == true) {
music.stop();
musicplaying = false;
} else {
music.stop(); // guard against overlap
music.start(0, 100);
musicplaying = true;
}
}

This way, you don't need to do any checking to figure out whether the sound is playing. You also need only one button, not two.

This script works with an attached or loaded sound, with the ID "music." When you start the sound file for the first time, you must initialize (set) the variable "musicplaying" to true:

music.start(0,100);
var musicplaying = true;

The script for a fully functional pause button, which stops and starts at the same spot in the audio file, is explained in the book.

> More tips