Archive
This is an archived tutorial about Flash. It's kept for history and may reference outdated tech. For a modern take, see Switch Statements in JavaScript.
TL;DR
switch is another way to write if/else in Flash MX. You put the expression to evaluate in the parentheses, list case values whose actions run when they strictly match, and finish with default for everything else. It's not faster, just easier to read, and it works with numbers, characters, and strings.
- End each case with break. Without it, Flash keeps reading the following lines, so a match on 0 also runs the default text.
- Each case acts like the if part of an if/else statement, while default plays the role of the else.
- A final example combines Key.addListener with switch(Key.getCode()) to trace which key was pressed last.
- The first example switches on Math.round(Math.random()*10), a random whole number between 0 and 10.
This is an archived tutorial from the kirupa.com legacy collection. It covers software that may no longer be available, but it is kept online because the ideas still hold up.
Bored with endless if... else ? Flash MX features a new way to deal with it: switch. This instruction already exists in several language (Jscript, PHP, C...) and allows you to evaluate expressions and is used as a substitute to if/else.
<h4>The Tutorial
As a matter of fact, switch()
doesn't bring anything new to Actionscript. It's simply a
way to write if/else differently. Here's how it works:
-
You define what you have to evaluate, between the () of the switch.
-
<h4>Between the {} of the switch, you put the actions to perform in the particular cases, that is to say when the expression following the case is evaluated as strictly equal to the expression that you "switched". This is equivalent to the if in an if/else statement. -
<h4>Last but not least, you define the default result (if none of the former result has come out). This is equivalent to the else in an if/else statement.
Here's an example of how it is used : put this in the first frame of a new movie.
Troubleshooting : Be careful that nothing should follow
the : in a case
Thus writing case
2:trace("Big big badaboom"); will come out as an
error. There must be at least one blank space.
How it works:
num =
Math.round(Math.random()*10);
num is a random number between 0 and 10. Basically,
Math.random returns a random number between 0 and 1, you
multiply it by 10, making it between 0 and 10, and then
Math.round rounds it into an integer.
switch(num) {
The number num is evaluated
case 0:
trace ("ZERO"); break ;
First case : is num equal to 0 ?
case 0:
trace ("ZERO"); break ;
If it's the case, you trace "ZERO". You can place here any
actions you want.
case 0: trace
("ZERO"); break ;
This takes you out of the switch. You HAVE to do this,
because otherwise Flash will try all the other case.
We don't want that to happen, as we will see later on.
And so on and so forth for the other case till we
reach the
default:
The actions following this statement will be executed if
none of the cases have been reached. Note that you don't
have to put break here.
In fact, in a switch, Flash reads all the lines one after
another. So let's imagine that num = 0. The first case will
trace "ZERO". If you don't put break, Flash will read the
case 2, case 4, case 6 which are evaluated as false, but the
default line is always evaluated as true.
The output will then be :
ZEROThe number is not equal to 0, 2, 4
or 8
There. As I said, switch doesn't bring anything new, it's
not faster or anything. It's just another way, easier to
read, to code.
Note that you can switch characters and strings just the
same.
<h4>Example
In this last example, you
will see how to check which key was pressed last. You can
find the code for each key in the context help of Flash.
This code uses a Listener, which will be the topic of
another tutorial.
That's it for this
tutorial. If you have any question regarding this tutorial,
you can find me on the board under the name pom, or
ilyaslamasse.pom
0] (archived resource unavailable)
Before you go, here are a few things you can do:
-
Keep reading
Read the next article in the series, Flash Snow 3.0, or check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence, ads, and algorithm-driven doodads. A huge thank you to all of you who buy kirupa's books, became a paid subscriber, watch the videos, and/or interact on the forums.
Your support keeps this site going! 😇
