Flash's flow control and loop control statements

Source: Internet
Author: User
Tags define copy execution expression final
Control | loops | The statement is well known, flash animation depends on the timeline, in the absence of script, the animation will be in accordance with the timeline from the first frame non-stop play to the last frame, and then play again or simply stop. In order to better control animation, you must use a scripting statement. And to make the animation has the logic to judge the function, we must use the flow control and the loop control statement! Here are a few simple examples to learn about process and loop control statements.

A, Process Control statement
The so-called process control, is that we want to control the animation program execution order. We can let flash decide which program to execute based on certain conditions. This seems to give the procedure some judgment mechanism.
If...else Control Statement
According to the meaning of English, we can simply understand the above control statement: If ... Just .... Otherwise...   Just .... Let's look at the writing format for this process Control statement.
if (condition) {
If the condition is true, execute the procedure here.
}else{
If the condition is not tenable, execute the procedure here.
}
The following highlights the conditions in parentheses after the IF. This condition can be a fixed value, or it can be a variable or an expression. If the condition is true (TRUE), the program after if is executed, if the condition is not valid, that is, if the condition is False (false), then the else program is executed. For example, if you have a condition that a>b this expression into parentheses after the IF, the process statement means: If a>b, the program in the first curly brace is executed, and if A is not greater than B, then the program in the curly brace behind else is executed.
Example Explanation:
1. Create a new flash document, draw a circle on the stage, select the Circle and press F8 to convert it into a movie clip, named: Circle This creates an instance of a circle on the stage.

2, select the instance on the stage, press F9 to open the Action panel, enter the following script:
Onclipevent (enterframe) {
if (this._x<400) {//Process condition control statement, the following program is executed if the x-axis coordinates of the instance of this circle are less than 400.
This._x + = 5;//Let the coordinates of this circle move 5 pixels to the right.
} else {
this._x = 0;//Control statement If just the condition is not tenable. That is, the X coordinate of the circle instance is not less than 400, let it be x coordinates 0
}
}
The script finally writes the effect as shown in figure:

Final Effect Demo: ( download source Files Click here)

The above script is a very simple process control statement, based on the coordinates of the instance to judge. Because we use the Enterframe event, Flash will keep executing the following if...else statement.
3, test the film. You will see that the circle is moving to the right, and when its x-coordinate is greater than or equal to 400, the program will change its x-coordinate back to 0.

Second, the circular control statement
The so-called circular statement, in fact, is also used to control the condition, as long as the conditions are set up, then, the program will continue to carry out, has been carried out until the conditions are not established until! Common loop statements include a while loop, a for loop, and so on. Because space is limited, the following focuses on the use of the For loop.
For loop command format:
for (initial variable; conditional statement; Iteration Command statement) {
Scripts written by the user themselves.
}
First of all, there are three items in parentheses in the For statement, which must be separated by semicolons!
Initial variables: Loop statements are also controlled by whether the condition is set up, and usually we use a variable to control the number of executions. So, this initial variable is going to first define a value. Note that the initial variable is only executed once for this project!
Conditional statement: This project is our judgment statement. If the project evaluates to True (TRUE), the condition is set. It jumps directly into the braces {} to execute inside the program! Conversely, if the condition is False (false), it immediately jumps out of the for statement.
The superposition command statement: The above conditional statement, if the conditional statement is established, will execute {} The program, then after executing the program, will come back to execute the superposition command statement. It is usually used to increase or decrease the value of the initial variable at first.
Example Explanation:
1. Create a new flash document, draw a circle on the stage, select the Circle and press F8 to convert it into a movie clip, named: Circle This creates an instance of a circle on the stage.
2, for this example named: Yuan as shown:

3, add a new layer, and select the first frame of the new layer, press F9 to open the Action panel, enter the following script:
For (I=1 i<5; i++) {//We define a variable I to control the number of loops in the program. Conditional statement to determine whether I is less than 5, the iterative command to allow each program to be executed, let I add one, so that the program Cycle 4 times!
Duplicatemovieclip ("Yuan", "Yuan" +i, i);//duplicatemovieclip is the copy command that replicates the instance named Yuan on the stage, each time one is copied, the newly copied instance is renamed "Yuan" +i. The depth of the film changes also to I
_root["Yuan" +i]._x = random (400);
_root["Yuan" +i]._y = random (300)//sets the x and Y coordinates of the new instance each time it is copied, and the numerical values are randomly generated.
_root["Yuan" +i]._alpha = random (100)//sets the transparency of the new instance each time it is copied, and the specific value is randomly generated from 100.
}
If you really think the for language is difficult to read, you can walk through the i=1 in the order in which they are executed to help you understand the program. For example: When I equals 1, the program begins to judge whether it is less than 5, then 1 is certainly less than 5, the condition is true, the replication program in {} is executed, and the name of the new instance that is copied becomes the Yuan1, then the yuan1 coordinates and the transparency are set. After that, the for statement starts back to execute the i++, at which point the value of I becomes 2. And then start the cycle of judgment execution ... (You can calculate, how many times does this for Loop loop in total?) The answer is 4 times. )
Script as shown:

4, select the 6th frame of the timeline, press F6 to insert a key frame, open the action panel to enter the following script:
gotoAndPlay (1);
Let Flash play to frame 6th and return to frame 1th to execute the FOR loop statement.
As shown in figure:

5, test the effect bar, you will see the stage will cycle 4 times to copy out 4 circle.
Final Effect Demo: ( download source Files Click here)

I hope you can grasp the Process Control statement and the circular control statement through these two simple examples

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.