Table of Contents

4.7 The Boolean Type

Boolean data is used to represent the logical states of truth and falsehood. Hence there are only two legal values of the boolean datatype: true and false. Notice that there are no quotation marks around the words true and false because Boolean data is not string data. The keywords true and false are reserved primitive data values and cannot be used as variable names or identifiers.

We use Boolean values with conditional expressions to add logic to the execution of code. For example, we might assign the value true to a variable that tracks the status of a spaceship's firepower:

shipHasDoubleShots = true;

By comparing shipHasDoubleShots to the Boolean literal true, we can decide how much damage to inflict when a shot hits its target:

if (shipHasDoubleShots =  = true) {
  // Shoot them with twice the power.
  // This will be reached if the comparison is true.
} else {
  // Shoot them with a single dose.
  // This will be reached if the comparison is false.
}

When the double-shot power runs out, we can set the variable to false:

shipHasDoubleShots = false;

This will cause the larger expression shipHasDoubleShots = = true to become false, causing the single-damage-dose script to execute when a shot hits its target.

All comparison operators express results with Boolean values. When we ask, "Is the user's guess the same as the password?" the answer is given as a Boolean:

// userGuess =  = password will yield either true or false
if (userGuess =  = password) {
  gotoAndStop("secretContent");
}

And when we ask, "Is the movie clip rotated more than 90 degrees?" the answer, again, is a Boolean:

// theClip_mc._rotation > 90 will yield either true or false
if (theClip_mc._rotation > 90) {
  // Fade out the clip if it's rotated past 90 degrees
  theClip_mc._alpha = 50;
}

Many internal ActionScript properties and methods describe the Flash movie environment in Boolean terms. For example, if we ask, "Is the spacebar being pressed?" the interpreter answers with a Boolean: true (yes) or false (no):

// Key.isDown( ) is a function that returns either true or false
if (Key.isDown(Key.SPACE)) {
  // Spacebar is being pressed, so make our spaceship fire
}

In Chapter 5, we'll learn how to phrase complex logical expressions using Boolean operators.

4.7.1 Using Boolean Values to Build a Preloader

Let's consider an applied Boolean example. Suppose we have a document with 500 frames and lots of content. The beginning of our opening sequence, frame 20, is labeled intro. We put the following code on frame 2 of that movie's main timeline:

if (this._framesloaded >= this._totalframes) {
  this.gotoAndPlay("intro"); 
} else {
  this.gotoAndPlay(1);
}

When the movie plays, the playhead passes frame 1 and enters frame 2. The ActionScript interpreter reaches the conditional statement and evaluates the Boolean expression within it: this._framesloaded >= this._totalframes. While the movie is still loading, this._framesloaded is less than the total number of frames in our movie (this._totalframes). If this._framesloaded is not greater than or equal to this. _totalframes, then the expression this._framesloaded >= this._totalframes, yields false. Therefore, the statement this.gotoAndPlay("intro") is skipped and the statement this.gotoAndPlay(1) is executed instead. The this.gotoAndPlay(1) statement sends the playhead back to frame 1 and plays the movie. When the playhead enters frame 2, our code is executed again. The playhead keeps looping in this way until the expression this._framesloaded >= this._totalframes yields the value true (i.e., until all the frames have loaded). At that point, the statement this.gotoAndPlay("intro"), which sends the playhead to the label intro, is executed. There we can safely start our movie, now that all of the frames have loaded.

Whammo! You've created a preloader based on a Boolean expression. This is solid stuff. We'll learn much more about conditionals and controlling movies with Booleans in Chapter 7.


Table of Contents