Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Syntax and Language Fundamentals > About statements > About conditions > Using a switch statement | |||
The switch statement creates a branching structure for ActionScript statements. Similar to the if statement, the switch statement tests a condition and executes statements if the condition returns a value of true.
When you use a switch statement, the break statement instructs Flash to skip the rest of the statements in that case block and jump to the first statement that follows the enclosing switch statement. If a case block doesn't contain a break statement, a condition called "fall through" occurs. In this situation, the following case statement also executes until a break statement is encountered or the switch statement ends. This behavior is demonstrated in the following example, where the first case statement doesn't contain a break statement and therefore both of the code blocks for the first two cases (A and B) execute.
All switch statements should include a default case. The default case should always be the last case on a switch statement and should also include a break statement to prevent a fall-through error if another case is added. For example, if the condition in the following example evaluates to A, both the statements for case A and B execute, because case A lacks a break statement. When a case falls through, it does not have a break statement, but includes a comment in the break statement's place, which you can see in the following example after case A. Use the following format when you write switch statements:
switch (condition) {
case A :
// statements
// falls through
case B :
// statements
break;
case Z :
// statements
break;
default :
// statements
break;
}
To use a switch statement in a document:
var listenerObj:Object = new Object();
listenerObj.onKeyDown = function() {
// Use the String.fromCharCode() method to return a string.
switch (String.fromCharCode(Key.getAscii())) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
/* E doesn't have a break statement, so this block executes if you press e or E. */
trace("you pressed E or e");
break;
case "I" :
case "i" :
trace("you pressed I or i");
break;
default :
/* If the key pressed isn't caught by any of the above cases, execute the default case here. */
trace("you pressed some other key");
}
};
Key.addListener(listenerObj);
Type letters using the keyboard, including the a, e, or i key. When you type those three keys, you'll see the trace statements in the preceding ActionScript. The line of code creates a new object that you use as a listener for the Key class. You use this object to notify the onKeyDown() event when the user presses a key. The Key.getAscii() method returns the ASCII code of the last key that the user presses or releases, so you need to use the String.fromCharCode() method to return a string that contains the characters represented by the ASCII values in the parameters. Because "E" doesn't have a break statement, the block executes if the user presses the e or E key. If the user presses a key that isn't caught by any of the first three cases, the default case executes.
|
|
|
|