home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeActionScript: The Definitive GuideSearch this book

4.7. The Boolean Type

Boolean data is used to represent the logical states of truth and falsehood. There are, hence, 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 the reserved primitive data values and may not be used as variable names or identifiers.

We use Boolean values 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 then 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) {

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

// myClip._rotation > 90 will yield either true or false
if (myClip._rotation > 90) {  
  // Fade out the clip if it's rotated past 90 degrees
  myClip._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, "Operators", 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 (_framesloaded >= _totalframes) {
} else {

When the movie plays, the playhead enters frame 2. The ActionScript interpreter reaches the conditional statement and evaluates the Boolean expression _ framesloaded >= _totalframes. While the movie is still loading, _framesloaded is less than the total number of frames in our movie ( _totalframes). If _framesloaded is not greater than or equal to _totalframes, then the expression _ framesloaded >=_totalframes, yields false. Therefore, the statement gotoAndPlay("intro") is skipped and the statement gotoAndPlay(1) is executed instead. The 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 _ framesloaded >= _totalframes yields the value true (i.e., until all the frames have loaded). At that point, the statement 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. Solid stuff. We'll learn much more about conditionals and controlling movies with Booleans in Chapter 7, "Conditionals".

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.