Sometimes you may want one button to do two different things at two different times, such as a toggle button that turns sounds on or off, or a button which stops or plays the movie.

You can do this by changing the button itself, such as hiding one button and showing another, or putting the button in a movie clip and jumping to another frame where it has another script. But a simpler way is to use a variable flag in the script to tell which state the button is currently in. Here's an example:

on (release) {    //  Toggle a flag between TRUE and FALSE:    IamActive = not IamActive;    //  Do different things depending on flag's current state:    if (IamActive) {       //  various actions to be performed for "on" state of button    } else {       //  various actions to be performed for "off" state of button    } }

When this first runs, the variable named IamActive has no value, and so is logically false. The first click makes it TRUE and the first section executes. On the next click the flag turns FALSE and the second section executes.

How to make one button do three different things

Here's a trickier handling, with a different approach. Attach the following script to a button. Also, place a Dynamic Text field on the stage with a variable name of debugField. For demonstration purposes, this sample simply outputs text intodebugField, but other actions can definitely be substituted:

on (release) {   //  Use the "modulus" operator (%) to increment between 1,2, and 3:   myCounter = (myCounter + 1) % 3;   if (myCounter == 1) {     debugField = "value is one";     } else {       if (myCounter == 2) {         debugField = "value is two";         } else {         debugField = "value is three";       }   } }




Keywords: tn_14807

This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License  Twitter™ and Facebook posts are not covered under the terms of Creative Commons.

Legal Notices   |   Online Privacy Policy