User Guide

Statements 215
switch statement
switch (expression){caseClause: [defaultClause:] }
Creates a branching structure for ActionScript statements. As with the if statement, the
switch statement tests a condition and executes statements if the condition returns a value
of
true. All switch statements should include a default case. The default case should include a
break statement that prevents a fall-through error if another case is added later. When a case
falls through, it doesn't have a break statement.
Availability: ActionScript 1.0; Flash Lite 1.0
Parameters
expression - Any expression.
Example
In the following example, if the
String.fromCharCode(Key.getAscii()) parameter
evaluates to
A, the trace() statement that follows case "A" executes; if the parameter
evaluates to
a, the trace() statement that follows case "a"executes; and so on. If no case
expression matches the
String.fromCharCode(Key.getAscii()) parameter, the trace()
statement that follows the
default keyword executes.
var listenerObj:Object = new Object();
listenerObj.onKeyDown = function() {
switch (String.fromCharCode(Key.getAscii())) {
case "A" :
trace("you pressed A");
break;
case "a" :
trace("you pressed a");
break;
case "E" :
case "e" :
trace("you pressed E or e");
break;
case "I" :
case "i" :
trace("you pressed I or i");
break;
default :
trace("you pressed some other key");
break;
}
};
Key.addListener(listenerObj);