ActionScript 3.0 Cookbook: Chapter 1: ActionScript Basics
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
The switch keyword is always
followed by the test expression in parentheses. Then the switch statement body is enclosed in curly
braces. There can be one or more case statements within the switch statement body. Each case (other
than the default case) starts with the case keyword followed by the case expression
and a colon. The default case (if one is included) starts with the
default keyword followed by a colon.
Therefore, the general form of a switch statement is:
switch (testExpression) {
case caseExpression:
// case body
case caseExpression:
// case body
default:
// case body
}
Note that once a case tests true, all the remaining actions in all
subsequent cases within the switch statement body also execute. This
example is most likely not what the programmer intended.
Here is an example:
var animalName:String = "dove";
/* In the following switch statement, the first trace( ) statement
does not execute because animalName is not equal to "turtle".
But both the second and third trace( ) statements execute,
because once the "dove" case tests true, all subsequent code
is executed.
*/
switch (animalName) {
case "turtle":
trace("Yay! 'Turtle' is the correct answer.");
case "dove":
trace("Sorry, a dove is a bird, not a reptile.");
default:
trace("Sorry, try again.");
}
Normally, you should use break statements at the end of each case body to exit the switch statement after executing the actions under the matching case.
The break statement terminates the current switch statement, preventing statements in subsequent case bodies from being erroneously executed.
You don't need to add a break statement to the end of the last case or default clause, since it is the end of the switch statement anyway.
var animalName:String = "dove";
// Now, only the second trace( ) statement executes.
switch (animalName) {
case "turtle":
trace("Yay! 'Turtle' is the correct answer.");
break;
case "dove":
trace("Sorry, a dove is a bird, not a reptile.");
break;
default:
trace("Sorry, try again.");
}
The switch statement is especially useful when you want to perform the same action for one of several matching possibilities. Simply list multiple case expressions one after the other. For example:
switch (animalName) {
case "turtle":
case "alligator":
case "iguana":
trace("Yay! You named a reptile.");
break;
case "dove":
case "pigeon":
case "cardinal":
trace("Sorry, you specified a bird, not a reptile.");
break;
default:
trace("Sorry, try again.");
}
ActionScript also supports the ternary conditional operator
(? :), which allows you to perform
a conditional test and an assignment statement on a single line. A
ternary operator requires three operands, as
opposed to the one or two operands required by unary and binary
operators. The first operand of the conditional operator is a
conditional expression that evaluates to either true or false. The second operand is the value to
assign to the variable if the condition is true, and the third operand is the value to
assign if the condition is
false.
varName = (conditional expression) ? valueIfTrue : valueIfFalse;
You want to make a decision based on multiple conditions.
Use the logical AND
(&&), OR (||),
and NOT (!)
operators to create compound conditional statements.
This excerpt is from ActionScript 3.0 Cookbook. Well before Ajax and Windows Presentation Foundation, Macromedia Flash provided the first method for building "rich" web pages. Now, Adobe is making Flash a full-fledged development environment, and learning ActionScript 3.0 is key. That's a challenge for even the most experienced Flash developer. This Cookbook offers more than 300 solutions to solve a wide range of coding dilemmas, so you can learn to work with the new version right away.
Many statements in ActionScript can involve conditional
expressions, including if, while, and
for statements, and statements using the
ternary conditional operator. To test whether two conditions are both
true, use the logical AND
operator, &&, as follows
(see Chapter
14 for details on working with dates):
// Check if today is April 17th.
var current:Date = new Date( );
if (current.getDate( ) == 17 && current.getMonth( ) == 3) {
trace ("Happy Birthday, Bruce!");
}
You can add extra parentheses to make the logic more apparent:
// Check if today is April 17th.
if ((current.getDate( ) == 17) && (current.getMonth( ) == 3)) {
trace ("Happy Birthday, Bruce!");
}
Here we use the logical OR
operator, ||, to test whether
either condition is true:
// Check if it is a weekend.
if ((current.getDay( ) == 0) || (current.getDay( ) == 6) ) {
trace ("Why are you working on a weekend?");
}
You can also use a logical NOT operator, !, to check if a condition is not
true:
// Check to see if the name is not Bruce.
if (!(userName == "Bruce")) {
trace ("This application knows only Bruce's birthday.");
}
The preceding example could be rewritten using the inequality
operator, !=:
if (userName != "Bruce") {
trace ("This application knows only Bruce's birthday.");
}
Any Boolean value, or an expression that converts to a Boolean, can be used as the test condition:
// Check to see if a sprite is visible. If so, display a
// message. This condition is shorthand for _sprite.visible == true
if (_sprite.visible) {
trace("The sprite is visible.");
}
The logical NOT operator is
often used to check if something is false instead of true:
// Check to see if a sprite is invisible (not visible). If so,
// display a message. This condition is shorthand for
// _sprite.visible != true or _sprite.visible == false.
if (!_sprite.visible) {
trace("The sprite is invisible. Set it to visible before trying this action.");
}
The logical NOT operator is often used in compound conditions along with the logical OR operator:
// Check to see if the name is neither Bruce nor Joey. (This could
// also be rewritten using two inequality operators and a logical
// AND.)
if (!((userName == "Bruce") || (userName == "Joey"))) {
trace ("Sorry, but only Bruce and Joey have access to this application.");
}
ActionScript doesn't bother to evaluate the second half of a
logical AND statement unless the
first half of the expression is true. If the first half is false, the overall expression is always
false, so it would be inefficient
to bother evaluating the second half. Likewise, ActionScript does not
bother to evaluate the second half of a logical OR statement unless the first half of the
expression is false. If the first
half is true, the overall
expression is
always
true .
You want to perform some task multiple times within a single frame.
Use a looping statement to perform the same task multiple times within a single frame. For example, you can use a for statement:
for (var i:int = 0; i < 10; i++) {
// Display the value of i.
trace(i);
}
When you want to execute the same action (or slight variations thereof) multiple times within a single frame, use a looping statement to make your code more succinct, easier to read, and easier to update. You can use either a while or a for statement for this purpose, but generally a for statement is a better choice. Both statements achieve the same result, but the for statement is more compact and more familiar to most programmers.
The syntax of a for statement consists of five basic parts:
for keywordfor
keyword.true or false. The test expression is
evaluated once each time through the loop. Generally, the test
expression compares the index variable to another value, such as
a maximum number of loop iterations. The overall expression must
evaluate to true for the
for statement's body to
execute (contrast this with a do...while loop, which executes at
least once, even if the test expression is false). On the other hand, if the test
expression never becomes false, you'll create an infinite loop,
resulting in a warning that the Flash Player is running
slowly.