The Actionscript Switch Case Statement

posted: 02-02-06
author: PrimeVector
website: http://www.free-webmaster-resource.com
views: 45851
comments: 1
total pages: 1
Download FlashMX2004 Source Files


The Actionscript Switch Case Statement

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.




Amazing Webmaster Widgets






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!



Latest News

Amazing Webmaster Tool

If you run a MySpace or teens related site with visitors interested in GLITTERS you will need this great tool: Webmaster Glitter Tool It will generate backlinks at an amazing pace!

 

Useful Click To Clipboard Script

This is a nice little copy to clipboard script. You can find it here!

 

Google Pages

Create pages on google quick and easy with Google Pages!

 

My Funny Pictures Site

Have fun on this funny pictutures site i made, i hope youll enjoy it :) PixHumor

 

IS YOUR CHILD SAFE???

With this great program you can make sure your child will be a lot safer while browsing the Internet.

 

New Site

I created a fun site for MSN Messenger Stuff called MSN Freak. You can find the site here: Free Winks @ MSN Freak

 

Useful free unlocker tool

This tool is a very useful tool that runs in the background. Whenever you try to for example copy a folder and explorer sceams at you it wont do that, the barrier is lifted and you can continue to do what you wanted too. Download it here.

 

GOOGLE worldwide: Languages and countries

Useful page showing all the Google Search Engines of all countries. Check it out here.

 

Google Trends

Google trends enables us to see where people want certain things the most. You can enter a keyword and find out where people search for that most often! Check Google Trends

 

Google PageRank Checker

I personally enjoy this Google pagerank checker a lot: Google PageRank Checker

 

New tutorials

I added a new basic Flash tutorial showing how to create a simple motion tween. You can find it here: Basic Flash Motion Tween

 

2 new Flash tutorials

I added 2 new Flash tutorials to the tutorial selection. On deals with the if else conditional statement and the other deals with setting up a bit of color with actionscript. You can checkout the tutorials here: Actionscript and colors and If Else Conditionals.

 

Oh My!

Its mad times overhere, im helping promote and have created a few sites which has taken quite some time from me. site1: http://www.flashgamefreak.com, site2 http://www.freewinks.net, site3 http://www.smilieworx.com and more are to come this and next week. This has taken its toll on my time to write tutorials. I will make up for that as soon as possible. Thanks for your understanding.

 

Basic Flash Tutorials

I added 3 new basic Flash tutorials to the tutorial list. Dealing with layers, What are functions and one about The Registration Point.

 

A bit of a delay

I have had a very crazy time around here the last month, because of this some tutorials have not been written yet. This is a list of tutorial i still need to write and you can expect them in the very near future.

 

Advertise on FWR

If you want to advertise here then please contact me here. We can make a nice deal and you can get stats on them how often you would like.

 

2 new tutorials

2 new web design related tutorials were added ... more Flash tutorials are on their way but its a bit mad around here these last days ;)

 

View All News



In Focus

Improve your home at Home Depot!
Join the Home Improver club today!
Do you shop at Home Depot?
Moving any time soon? Home Depot has the supplies that you need for any move or any other occasion.
Free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Make cash with Penny Stocks!
Make cash with Penny Stocks!
Save money on your Auto Insurance!
You could be paying too much for your auto insurance, find out here if you qualify for HUGE savings!
Get free Auto Insurance Quotes!
Saving money with InsureMyCar4Less is simple. After you fill out our quick online form, you get free auto insurance quotes from the best providers in the industry. They compete for your business, and you save money.
Play World War 2 Commando Free!
Play World War 2 Commando online for free!
Play Aztec Quest online for free!
Play Aztec Quest online for free!
Play Swat 2 Now
Play the swat 2 game and shoot those bad guys!
Play Bubble Boomers
Pop the bubbles of the like colors for this very fun and addictive game for FREE!!!
Play Frogger Now!
Play frogger with 1 click download of gamevance!
Play Airstrike 1941 online for free!
Play Airstrike 1941 online for free!
Get a Free Auto Insurance Quote
Free quotes from top insurance carriers!
Customize your Facebook with a theme
Customize the colors and music on your facebook profile page today!
Save up to 70% on auto insurance!
Do you qualify for 60-70% off your auto insurance rates? Find out right now! Request quotes 24 hours a day with our secure online form. It's super fast and easy. See what your rate is absolutely free!
Do you shop at Home Depot?
Get a ton of Home Depot coupons and gift card offers! Also if your looking to improve you home this is the place to be!
Play Pac Man right here!
Get all the hottest new games and all the old school games in one place!
Play Streetfighter Now!!!
Get all the hottest new games and all the old school games in one place!
Play the old school Baseball Stars
Get all the hottest new games and all the old school games in one place!
Print Coupons from Home!
Print Grocery Coupons from your house! Instant Savings!
Want Home Depot September Deals?
Get some great September deals at Home Depot today!
Share your Opinion, win $50,000?
Share your Opinion, win $50,000? Couldn't get any easier! It's free and fun!
Stop identity theft in its tracks!
Protect My ID makes it easy for you!
Need New Contact Lenses for FREE?
Treat your eyes to all day comfort with a free trial and save up to $100!
Test Hunting Gear and Keep It!
We're looking for a few good product testers for our Hunting Gear!
Save Money on Airline Fares Here!
Save up to 50% off on major airline brands here!
Get a FREE Apple iPad Now!!!
You know you want your FREE Apple iPad right here!
Do you like to go Fishing?
We're looking for a few good product testers to test our Fishing gear. Keep it afterwards!

Cool Stuff