3.4.2 Comparison
It is easy to compare two things in ActionScript. To compare, standard mathematical symbols such as =, <, >, etc.
1. equal
Comparison operators for two values in ActionScript.
To compare whether two values are equal, you can use two equal signs (==) that are concatenated together. A single equality symbol (=) is used to assign values to a variable, not a comparison operator.
If you want to compare whether the value of variable x is equal to 7, you can use the == sign as follows:
var x = 7;
trace(x == 7);
The above code uses the = symbol to set the variable x to 7, and then uses the == symbol to compare x and 7.
Test these two lines of code and the output window will display "true". If x is set to 8 or another number, "false" will be displayed.
The == symbol can also be used to compare two strings. As shown below:
var myString = "Hello ActionScript.";
trace(myString == "Hello ActionScript.");
trace(myString == "hello ActionScript.");
When the program is running, you will see a "true" and a "false" in the output window because the letters are case sensitive in the string.
If you want to compare whether two values are not equal, you can use the != symbol, which means "not equal". As shown below:
var a = 7;
trace(a != 9);
trace(a != 7);
The first trace statement shows the message "true", because a does not equal 9; the second trace statement shows the message "false".
2. Less than and greater than
Use the standard mathematical symbols < and > to compare whether the two numbers are less than or greater than the relationship. As an example:
var a = 9;
trace(a < 10);
trace(a > 5);
trace(a < 1);
You will see "true", "true" and "false" from the output window.
The symbol <= or >= is used to compare whether a number is less than or equal to another number, as shown below:
var a = 9;
trace(a <= 9);
trace(a >= 9);
trace(a >= 7);
The above three trace statements will display "true".
3.4.3 Operation
The value of a variable can be changed through operations. Use the arithmetic operators +, -, *, / to perform addition, subtraction, multiplication, and division operations respectively.
ActionScript as shown below adds a variable x with a value of 9 plus a number 7:
var x = 9;
x = x + 7;
trace(x);
The calculation result is 16.
In ActionScript, you can use some abbreviation methods, such as the += operator that adds the values before and after it and assigns the result to the variable before it. The previous script can also be written in the following form:
var x = 9;
x += 7;
trace(x);
The ++ operator is similar to the += operator, but every time it is executed, the value of the variable is only increased by 1, as in the following example:
var x = 9;
x++;
trace(x);
The results show 10. Let’s take a look at the following example:
var x = 9;
trace(x++);
trace(x);
The results are 9 and 10. Why? Because the first trace statement outputs the current value of x 9, then adds x 1, outputs the new value of x 10.
Try the following script again:
var x = 9;
trace(++x);
trace(x);
This time the result is two 10. Because the ++ operator is placed before the variable, the operation will be performed first and then the value of the variable will be returned.
Similarly, the -- operator performs a decrement operation, the -= operator subtracts a number based on the current value of the variable, the *= operator multiplies a number based on the current value of the variable, and the /= operator divides a number based on the current value of the variable by a number.
3.4.4 Conditions
Now that you already know how to compare two variables, you can use the comparison result as a condition for executing certain statements.
1. if statement
The if statement allows you to use the comparison results to control the playback of the Flash movie. The following statement determines whether X is equal to 9. If the comparison result is true, let the video jump to frame 15:
if (x == 9) {
gotoAndPlay(15);
}
The if statement starts with if, followed by a comparison expression. The comparison expression is usually enclosed in a pair of brackets, followed by the code to be executed when the comparison result is true.
2.else
The if statement can be extended. Use else to execute the code when the condition is not true (the comparison expression is false), as shown below:
if (x == 9) {
gotoAndPlay(15);
} else {
gotoAndPlay(16);
}
You can also use the else if statement to push the if statement further, as shown below:
if (x == 9) {
gotoAndPlay(15);
} else if (x == 10) {
gotoAndPlay(16);
} else if (x == 11) {
gotoAndPlay(20);
} else {
gotoAndPlay(25);
}
You can make the if statements go as long as they want, and you can also use the else if statement to compare other variables as shown below:
if (x == 9) {
gotoAndPlay(15);
} else if (y<20) {
gotoAndPlay(16);
} else {
gotoAndPlay(25);
}
3. Compound comparison
You can judge the values of several comparison expressions in an if statement. For example, if you want to jump to frame 10 when x is 9 and y is 20, you can use the script shown below:
if ((x == 9) && (y == 20)) {
gotoAndPlay(10);
}
Logic and operator && joins two comparison expressions together to form a composite expression. The value of the composite expression is true when both expressions have values true. Each comparison expression needs to be added with independent brackets so that Flash can correctly identify it. Use and perform logic and operations in earlier versions of Flash, and is now not recommended.
You can also use logic or operator || to join two comparison expressions together into a composite expression. As long as one expression has a value of true, the value of the composite expression is true. As shown below:
if ((x == 7) || (y == 15)) {
gotoAndPlay(20);
}
In this script, as long as x is 7 or y is 15, or both are true, the result will be jumped to frame 20. The gotoAndPlay command will not be executed only when neither is true. Use or to perform logic or operations in earlier versions of Flash, and is now not recommended.
3.4.5 Cycle
Loops in ActionScript are slightly more complicated than if statements. Its loop structure is almost consistent with the loop structure in C language.
1. for loop structure
The for structure is the main loop structure, and its style is as follows:
for (var i = 0; i<10; i++) {
trace(i);
}
Run this code and as the local variable I change, the numbers 0~9 will be displayed in the output window.
The brackets after the keyword for in the for structure contain 3 parts, separated by semicolons.
Part 1 declares a local variable, in this case a local variable i is created and set it to 0. This part is only executed once before the loop body begins to execute.
Part 2 serves as a condition for testing, where test whether i is less than 10. As long as this condition is met, the loop body will be repeatedly executed. When the loop starts, i is equal to 0, it is less than 10, so the loop is executed.
Part 3 is an operational expression, which will be executed once every time the loop is completed. Here, i is incremented by 1 each time, and then go to the 2nd part to judge the new value of i.
The part in the braces is the loop body of the loop, and every time the loop is executed, all the commands in it will be executed. Let's see how the computer handles this loop.
Declare the variable i and set its value to 0;
The judgment condition i<10, and the result is true, and the loop body is started;
Now that the i value is 0, the trace command sends the i value to the output window, and the output window displays 0;
The first cycle ends, and returns to the beginning of the cycle, i increases by 1 on the original basis, and the value of i becomes 1;
The judgment condition i<10, and the result is true, and the loop body will continue to be executed;
The trace command sends the value 1 of i to the output window;
Then add 1 to i and loop this way until 10 cycles are executed;
Return to the beginning of the loop, i increments by 1 on the original basis, and the value of i becomes 10;
The judgment condition i<10, the result is false, the loop ends, and the code behind the for structure begins to be executed.
2. Other forms of loop structure
The for loop is the most commonly used loop structure. In addition to the for loop, there are also while loops and do...while loops.
An example of a while loop is as follows:
i = 0;
while (i != 10) {
trace(i);
i++;
}
While loops seem to be simpler than for loops, and they are even somewhat similar to if statements in terms of structure. As long as the conditions in the brackets after while are true, the loop will continue, so there are statements that change the conditions in the while loop body so that the condition can finally be false and complete the loop, such as i++ in the above example.
Similar to a while loop is a do...while loop, as shown below:
i = 0;
do {
trace(i);
i++;
} while (i != 10);
Except for the location of the test conditions, the while loop and the do...while loop are almost the same. The while loop tests the conditions before the loop body, and the do...while loop tests the conditions after the loop body, so the do...while loop must be executed at least once, and the while loop may not be executed once.
3. Break out of the loop
All loop structures can use two commands to change the execution flow of the loop, one is break and the other is continue. The break command terminates the loop and jumps to the statement behind the loop structure to execute; the continue command terminates the loop but does not jump out of the loop, and then executes the next loop.
Examples of using break and continue are quite complex. In addition, there is a special for…in loop structure, which we will explain in detail when using them.
3.4.6 Function
Until now, we have put the script in frame 1 of the video. If the program is quite complicated, putting it in the same frame will make the script appear too large.
Functions allow you to organize the code you want to reuse and put it in the timeline, for example:
function myFunction(myNum) {
var newNum = myNum+5;
return newNum;
}
The function begins with the keyword function, followed by the function name. Similar to variable names, you can specify your own function name, it is best to get the function name more meaningful.
The brackets after the function name hold the parameters of the function. The so-called parameter is also a variable, and its value is specified when the function is called. A function can have several parameters or no parameters. Regardless of whether there are or not, the function name should be followed by a pair of brackets.
The part in the braces is the function body. A local variable newNum is created in the function body. The result of adding 5 to the value of newNum. If you pass 10 as an argument to the function, the value of newNum is 15.
The return command is only used in functions. Use return to end a function and return the function value. Here, newNum is the function value returned by the return command.
To use a function, you need to call it as follows:
var a = myFunction(7);
This statement creates a new local variable a, calls the function myFunction as a parameter, and takes the result returned by the function as the value of the variable a.
The called function starts running, creates a local variable myNum, takes 7 as the value of myNum, and then executes the code inside the function body, and uses the return command to return the value of newNum 12 to the caller of the function. At this time, the value of a becomes 12.
The biggest function is reflected in its reusable function. The 3 lines of code shown below produce 3 different results:
trace(myFunction(3));
trace(myFunction(6));
trace(myFunction(8));
Run the above code and you will get results 8, 11 and 13.
Another advantage of using a function is that it can only change one of the functions, thereby affecting all commands that call the function. For example, change var newNum = myNum+5 in the function myFunction to var newNum = myNum+7, the results of the above 3 commands calling the function will become 10, 13 and 15
3.4.7 Point syntax
A very important concept in ActionScript is point syntax. Point syntax is also a method used to organize objects and functions in many object-oriented programming languages.
Suppose you want to find the absolute value of a number, there is a built-in absolute value function in Flash, which is included in ActionScript's "Object"/"Core"/"Math"/"Meth"/"Meth" in ActionScript. To use an absolute value function, first use the object name, namely Math, and then the method name abs, separated by the symbol ".", the specific expression method is as follows:
var a = (-7);
Another use of dot syntax is to specify the properties of the movie clip. For example, the following statement sets the _alpha (transparency) property of the movie clip myMC to 50%:
myMC._alpha = 50;
You can also use point syntax to locate a global variable in the root (root) in movie clips. If you create a global variable globelVar in the main timeline and want to use this global variable in the movie clip, you can use the following statement:
trace(_root.globleVar);
If you are not familiar with concepts such as objects, video editing attributes, and levels, don’t worry, we will explain them more in-depth in the future content.
3.4.8 Comment
We can add comments in the action panel of Flash, which are the codes in the program that are not involved in execution. It can be used to remind you of the role of certain code, making it easier for you to organize and write scripts. An example of annotation is as follows:
// Set the transparency of the video clip myMC to 50%
myMC._alpha = 50;
The first line of this example is a comment. The comment starts with a double slash //. After // you can enter any text and symbols. Flash will automatically mark the comment part in gray.
The above example is to place comments specifically in one line, and you can also place comments after a line of code, as shown below:
myMC._alpha= 50; // Set the transparency of the video clip myMC to 50%
As long as the // symbol is used, Flash will ignore the part behind it.
3.5 Debug scripts
No matter how careful you are when writing programs, every programmer cannot avoid debugging programs. To learn ActionScript well, you must master the methods of debugging programs. There are three ways to debug a program in Flash MX: logical inference, sending information to the output window, and using a script debugger.
3.5.1 Logical inference
Many program errors are actually very simple and easy to solve, and do not require special debugging tools. When an error occurs in the program, you should have a rough grasp of where the error occurs.
Even if you don't know where the problem lies, you can find it by carefully reading the code. You can think about what the effect you want to achieve? How far is the difference between the current effect and your idea? What causes this gap? What parts are modified that are expected to correct this error? After repeated thinking, modification and debugging, your understanding of the program will continue to deepen and your program will become more and more correct.
No one knows your own program better than you, so for you, the program you write should first be debugged using logical inference methods. There are also some errors or vulnerabilities that are hidden deep, which requires the help of debugging tools.
3.5.2 Output Window
The output window is a simple and practical debugging tool. In the program, you can use the trace command to send specific information to the output window. This information can help you understand the running of the code.
We have introduced the output window before, so I won’t go into details here.
3.5.3 Debugger
The debugger is a more professional debugging tool. It is a window in Flash MX. Through the debugger, you can view various data in the video and the operation of ActionScript code.
Select the "Control" → "Debug Video" command, or press the shortcut key Ctrl+Shift+Enter to start debugging the video. Unlike test videos, there is an additional debugger window when debugging the video, as shown in Figure 3-4.
Figure 3-4 Debugger window
The debugger window contains many panes. You can check different types of objects in the Flash movie in the pane on the left, and the pane on the right shows all ActionScript in the movie.
You can set a breakpoint in the debugger window. The breakpoint is a mark added to a line of code. When debugging a movie, the movie will automatically stop at the breakpoint, allowing you to view the current movie status including the variable value. Using breakpoints allows you to execute code line by line, observing and analyzing the running results of each line of code.
When you are new to ActionScript, you may not need to use a debugger; but when you grow into an experienced programmer, you will find it very useful to you.
Previous page12Read the full text