The Switch Case Statement is a very useful way to setup a conditional check instead of using if... else...
The Switch Case has a much cleaner setup and thus is way better to read when you have to setup
a lot of conditional checks. This means less chance to run into errors or when you open your script a year later you
can read it without too much trouble because of the way the Switch Case is setup. (its very descriptional)
Below i created a small example using numbers. We setup a want to trigger certain stuff when certain
condinions meet our requirements.
In our case we will trigger an output message to your screen when ran in Flash itself.
The Actionscript Code With Comments
First we setup a variable called "number" and we fill this with a random number (0,1,2,3,4).
I used Math.round(value); to round the number down to a full integer. Then i used Math.random()* number,
to generate a random value.
var number = Math.round(Math.random()*4);
Now we open our Switch Case Statement and we plugin variable 'number'.
switch (number) {
Here we setup the first case. Because we want something to happen (in our case a
trace output) when the variable 'number' is set to 0, we set it up as a case. The
format for case is "case condition:code to trigger"
case 0:
This outputs the text "variable 'number' is set to 0" if ran in Flash. At this point
you could do anything really, you could call other function and execute all sorts
of code. I hope you see how useful this is. The Switch Case Statement checks
'number' and if it is set to 0 (or whatever we want it to be) it triggers.
trace ("variable 'number' is set to 0");
Because the variable 'number' was found to be set to 0 we can stop checking
and we do this by using 'break'. This just stops the checkup.
break ;
Now we define case 1. This outputs the text "variable 'number' is set to 1" if ran in
Flash and if 'number' is set to 1.
case 1:
This outputs the text "variable 'number' is set to 1" if ran in Flash.
trace ("variable 'number' is set to 1");
Because the variable 'number' was found to be set to 1 we can stop checking
and we do this by using 'break'.
break ;
Now we define case 2. This outputs the text "variable 'number' is set to 2" if ran in
Flash and if 'number' is set to 2.
case 2:
This outputs the text "variable 'number' is set to 2" if ran in Flash.
trace ("variable 'number' is set to 2");
Because the variable 'number' was found to be set to 2 we can stop checking
and we do this by using 'break'.
break ;
Now we define case 3. This outputs the text "variable 'number' is set to 3" if ran in
Flash and if 'number' is set to 3.
case 3:
This outputs the text "variable 'number' is set to 3" if ran in Flash.
trace ("variable 'number' is set to 3");
Because the variable 'number' was found to be set to 3 we can stop checking
and we do this by using 'break'.
break ;
Here we setup what should happen when the conditions we have setup are never
met.
So for example when the variable 'number' rendered to 4 this would trigger.
default:
As a default trace we trace the string "The number is not equal to 0, 1, 2 or 3".
trace ("The number is not equal to 0, 1, 2 or 3") ;
Here we close the Switch Case Statement.
}
The Actionscript Code Without Comments
Below you can find the full Actionscript code without the comments.
var number = Math.round(Math.random()*4);
switch (number) {
case 0:
trace ("variable 'number' is set to 0");
break ;
case 1:
trace ("variable 'number' is set to 1");
break ;
case 2:
trace ("variable 'number' is set to 2");
break ;
case 3:
trace ("variable 'number' is set to 3");
break ;
default:
trace ("The number is not equal to 0, 1, 2 or 3") ;
}
Conclusion
Personally i like the Switch Case a lot. Its a very descriptional piece of code and after you play around with
it a bit you can understand it quite quickly and do some cool things with it.
I hope you could follow along with this tutorial, but if this was not the case please dont hestitate to drop your
question at the
forums as it is much easier to help there instead of the comment box below. Thanks.
Comments
1 comment(s) found in 1 page
viewing comment(s): 1 - 1
1 |
02-02-06:PrimeVector
new
- Press here to view all available smilies!