Tutorials Books Videos Forums

-- online Change the theme! Search!
Rambo ftw!

Customize Theme


Color

Background


Done

Flash MX: Switch / Case

by ilyas usal a.k.a. pom   | filed under Flash and ActionScript

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:

  1. You define what you have to evaluate, between the () of the switch.

  2. <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.
  3. <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.

                      num = Math.round(Math.random()*10);
switch (num) {
 case 0:
  trace ("ZERO"); break ;
 case 2:
  trace ("TWO"); break ;
 case 4:
  trace ("FOUR"); break ;
 case 8:
  trace ("HEIGHT"); break ;
 default:
  trace ("The number is not equal to 0, 2, 4 or 8") ;
 }
 

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.

                      letter = 'a' ;
switch (letter) {
 case 'a':
  trace ("A"); break ;
 case 'b':
  trace ("B"); break ;
 case 'c':
  trace ("C"); break ;
 default:
  trace ("The letter is not a, b or c") ;
 }
 

<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.

                      // _root "listens" to keyboard events
Key.addListener(_root);

// Now that _root. listens, we can define the function
// executed when we press a key :
_root.onKeyDown = function(){

// What was the last key pressed ?
switch(Key.getCode()){
 case 65: trace("A"); break;
 case 66: trace("B");break;
 case 67: trace("C");break;
 case 68: trace ("D");break;
 default: trace ("Not A, B, C nor D");
 }
}
 


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]

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! 😇

The KIRUPA Newsletter

Thought provoking content that lives at the intersection of design 🎨, development 🤖, and business 💰 - delivered weekly to over a bazillion subscribers!

SUBSCRIBE NOW

Creating engaging and entertaining content for designers and developers since 1998.

Follow:

Popular

Loose Ends

:: Copyright KIRUPA 2026 //--