Understanding the ActionScript Language > About scripting in ActionScript > How scripts flow |
![]() ![]() ![]() |
How scripts flow
Flash executes ActionScript statements starting with the first statement and continuing in order until it reaches the final statement or a statement that instructs ActionScript to go somewhere else.
Some actions that send ActionScript somewhere other than the next statement are if
statements, do..while
loops, and the return
action.
A flow chart of the if..else
action
A flow chart of the do..while
action
An if
statement is called a conditional statement or a "logical branch" because it controls the flow of a script based on the evaluation of a certain condition. For example, the following code checks to see if the value of the number
variable is less than or equal to 10. If the check returns true
(for example, the value of number
is 5), the variable alert
is set and displays its value, as shown here:
if (myNumber <= 10) { alert = "The number is less than or equal to 10"; }
You can also add else
statements to create a more complicated conditional statement. In the following example, if the condition returns true
(for example, the value of number
is 3), the statement between the first set of curly braces runs and the alert
variable is set in the second line. If the condition returns false
(for example, the value of number
is 30), the first block of code is skipped and the statement between the curly braces after the else
statement runs, as in the following:
if (number <= 10) { alert = "The number is less than or equal to 10"; } else { alert = "The number is greater than 10"; }
For more information, see Controlling flow in scripts.
Loops repeat an action a certain number of times or until a certain condition is met. In the following example, a movie clip is duplicated five times:
i = 0; do { duplicateMovieClip ("myMovieClip", "newMovieClip" + i, i); newName = eval("newMovieClip" + i); setProperty(newName, _x, getProperty("myMovieClip", _x) + (i * 5)); i = i + 1; } while (i <= 5);
For detailed information, see Repeating an action.
![]() ![]() ![]() |