switch
211
switch
Availability
Flash Player 4.
Usage
switch (
expression){
caseClause
:
[
defaultClause
:]
}
Parameters
expression
Any
expression.
caseClause
A
case
keyword followed by an expression, a colon, and a group of statements to
execute if the expression matches the switch
expression
parameter using strict equality (
===
).
defaultClause
A
default
keyword followed by statements to execute if none of the case
expressions match the switch
expression
parameter strict equality (
===
).
Returns
Nothing.
Description
Statement; 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.
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" :
CHAPTER 5
ActionScript Core Language Elements
Summary of Contents for FLEX-FLEX ACTIONSCRIPT LANGUAGE
Page 1: ...Flex ActionScript Language Reference...
Page 8: ......
Page 66: ...66 Chapter 2 Creating Custom Classes with ActionScript 2 0...
Page 76: ......
Page 133: ...break 133 See also for for in do while while switch case continue throw try catch finally...
Page 135: ...case 135 See also break default strict equality switch...
Page 146: ...146 Chapter 5 ActionScript Core Language Elements See also break continue while...
Page 808: ...808 Chapter 7 ActionScript for Flash...
Page 810: ...810 Appendix A Deprecated Flash 4 operators...
Page 815: ...Other keys 815 Num Lock 144 186 187 _ 189 191 192 219 220 221 222 Key Key code...
Page 816: ...816 Appendix B Keyboard Keys and Key Code Values...
Page 822: ...822 Index...