Toggling a Boolean Value

Toggling a Boolean Value

Flash CS4 ActionScript 3.0 Tutorial

 

How to Create a Variable with True or False States with Flash ActionScript

 

 

Boolean variables can be set to one of two states, true or false. They are handy when working with conditional statements. Boolean values can also be toggled back an forth between a true or false value., In this example we will demonstrate how to "flip" the Boolean value.

 

Step One

Open a new document and name the default layer, Actions.

Step Two

Option double-click (PC - Alt double click) the first keyframe of the Actions layer to open the ActionScript panel.

Step Three

Copy/Paste the following code in the ActionScript Panel:

var bolValue:Boolean=false;

trace("Initial Boolean Value = " + bolValue);

bolValue = !bolValue

trace("Boolean reversed once= " + bolValue);

bolValue =! bolValue

trace("Boolean reversed again= " + bolValue);

Step Four

Test your movie by pressing Cmd/Return (PC - Ctrl/Enter). The trace statements appear in the Output panel.

Dissecting the Code

The first line establishes a new Boolean variable named bolValue. The next line traces the current value of bolValue in the Output panel.

The next lines convert the false value of bolValue to true. That value is then displayed in the Output panel.

The final lines convert the true value of bolValue to false. That value is then displayed in the Output panel. Note the spacing between the = and ! symbols. You can place the space between the symbols or between the exclamation mark and the variable.

 

Stock Images