How to make a toggle button

February 27, 2010

How To

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.

, , , , , ,

Subscribe

Subscribe to our e-mail newsletter to receive updates.

6 Responses to “How to make a toggle button”

  1. Alex Says:

    Great tut!

  2. hmunglian Says:

    hi i learn how to create toggle button in flash but what if you want to use in webisite
    or use in Dreamweaver for website..

    thanks

Trackbacks/Pingbacks

  1. How to make a toggle button | Flash tutorials | Flash video … at Flash Designers - February 27, 2010

    [...] posted here: How to make a toggle button | Flash tutorials | Flash video … [...]

  2. Exclusive: BSG’s Gaius Baltar Coming to FlashForward | WeCharts.com - February 27, 2010

    [...] How to make a toggle button | Flash tutorials | Flash video … [...]

  3. Google Maps API Basic Tutorial | Tutorialicious.info - February 27, 2010

    [...] How to make a toggle button | Flash tutorials | Flash video … [...]

  4. How To Create A Set Of Stylish Social Bookmarking Icons | Tutorialicious.info - February 27, 2010

    [...] How to make a toggle button | Flash tutorials | Flash video … [...]

Leave a Reply