Saturday, November 13, 2010

Switch case in AS3

Today we will learn about the switch case in Actionscript 3.

Using switch is just another way of using if...else. First you define what you want to evaluate and then give different cases of what it could be and what action should be taken.

Here is an example:

switch (myVariable){

case "Hello":
trace("My variable is a greeting text");
break;

case 50:
trace("My variable is number 50");
break;

default:
trace("My variable is neither a greeting text, nor the number 50.");
}

Enter a variable in the ( ) that you want to evaluate and add different cases like in the example above. Each case must be finnished by break;. The default section defines what happens when the variable does not match any of these cases.

This is often seen as a more easier and shorter way of writing many if...else statements.

Hope this was helpful. Thanks for reading!

No comments:

Post a Comment