O'Reilly logo

Learning ActionScript 3.0 by Zevan Rosser, Rich Shupe

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Conditionals

You will often have the need to make a decision in your script, choosing to do one thing under one circumstance, and another thing under a different circumstance. These situations are usually addressed by conditionals. Put simply, a test is created, asking whether a condition is met. If the condition is met, the test evaluates to true, and specific code is executed accordingly. If the condition is not met, either no further action is taken or an alternate set of code is executed.

Note

Throughout this book, the code examples are syntax-colored in the same way that the Flash interface colors scripts. This helps identify colored items as part of the ActionScript lexicon (such as keywords and identifiers) and makes it a bit easier to see comments (descriptive text passages that are not executed) and strings.

if

The most common form of the conditional is the if statement. The statement's basic structure is the if keyword, followed by parentheses in which the conditional test resides, and braces in which the code resides that is executed when the statement evaluates to true. The first three lines in the following example establish a set of facts. The if statement evaluates the given facts. (This initial set of facts will be used for this and subsequent examples in this section.)

var a:Number = 1;
var b:String = "hello";
var c:Boolean = false;

if (a == 1) {
    trace("option a");
}

To evaluate the truth of the test inside the parentheses, conditionals often make use of comparison and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required