Flash Switch Case instance tutorial

Source: Internet
Author: User
Tags comments switch case

Flash Switch Case instance tutorial

Tired of endless, If... anything else? Flash MX adds a new way to handle this problem: switch. This indicator already exists in some languages (scripting language, PHP corner...) and allows you to express and comment as an alternative if/by person.

This tutorial
In fact, the switch () won't bring any new scripts. It is just a way, if the write/others are different. Its working principle is as follows:
Are you sure you have any comments between () switch.
Between () switches, you will take action to fulfill in specific circumstances, that is, when the expression is evaluated as strictly equal expression, you "exchange ". This is equivalent to a statement such as/else.
Last but not least, you define the preset result (if there is no previous result ). This is equivalent to the/else statement for others.
Here is 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 and never follow: in a case
Therefore, written Case 2: Tracking ("Big badaboom"); will come out as an error. There must be at least one blank space.

The principle is:

--------------------------------------------------------------------------------

Number = Math. round (Math. random () * 10 );
A random number is between 0 and 10. Basically, Math. random returns a random number between 0 and 1. You multiply it by 10 to be between 0 and 10, and then Math. round turns it into an integer.

Switch (number )(
Rating

Case 0: trace elements ("0"); break;
The first case is: the number is equal to 0?

Case 0: trace elements ("0"); break;
In this case, you track "zero ". You can take any action here, what you want.

Case 0: trace elements ("0"); break;
This requires your switch. To do this, because otherwise Flash will try all other situations. We don't want this to happen because we will see it later.

And so on until we reach

Default value:

The following statement of action will be executed if no such cases have been reached. Please note that you do not have to break it here.
In fact, in a switch, all the flash content is tied to one by one. As we can imagine, the number is 0. In the first example, "Zero" will be tracked ". If you do not take the break, flash will read the case 2 case 4 case 6 day is false comments, but the default line is always as true comments.

Output and then:

ZEROThe number is not equal to 0, 2, 4 or 8

There. As I said, the switch won't bring anything new, it's not faster or anything. This is just another way to make it easier to read and code.

Note that you can switch between the same character and string.

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 ");
 }

For example
In the last example, you will see that the key to how to check is to press over. You can find every key to the code and flash it with help. This code uses a listener, which is another topic 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 ");
 }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.