Operators


Operators are special symbols we can enter into a script that allow us to manipulate variables. The variables they operate on are called operands. Some examples of operators include + , -, * , / , < , > , = , & , , && , and . A full list of operators is shown later in Table 2.1, as well as in the ActionScript reference in Appendix A, "ASCII Character Chart."

 
Table 2.1: Operator Precedence

Operator

Description

+

Unary Plus (positive number)

-

Unary Minus (negative number)

!

Logical not

++

Post-increment

--

Post-decrement

()

Grouping or function call

[]

Array element

.

Member access

++

Pre-increment

--

Pre-decrement

new

Allocation

delete

Deallocation

typeof

Type of object

void

Undefined type

*

Multiplication

/

Division

%

Modulus

+

Addition

-

Subtraction

<<

Bitwise shift left

>>

Bitwise shift right

>>>

Bitwise shift right (unsigned)

<

Less than

<=

Less than or equal to

>

Greater than

>=

Great than or equal to

==

Equivalence (is equal to)

!=

Is not equal to

&

Bitwise AND

^

Bitwise XOR

Bitwise OR

&&

Logical AND

Logical OR

?:

Conditional

=

Assignment

*=, /=, %=, +=, -=, &=, =, ^=, <<=, >>=, >>>=

Compound assignments

Some operators will seem simple to you, whereas others will be less intuitive. The arithmetic operators, for example, should be easy because you've been using them since elementary school. These operators include addition, subtraction, multiplication, and division. The following script illustrates those four operations:

 result1 = 5 + 3; result2 = 7 - 4; result3 = 3 * 2; result4 = 7 / 3; 

That script tells Flash to create four variables named result 1-4 . Each one is assigned the result of some arithmetic operation. The plus symbol is used for addition, the minus symbol is used for subtraction, the asterisk is used for multiplication, and the forward slash is used for division. To confirm the results of these operations, you could use the following script, which would be placed after the arithmetic. If you add this script, you should have output that looks like Figure 2.9:

Note  

All script, unless otherwise mentioned, goes in frame 1 of the main movie timeline. That's where the vast majority of our script will go; in the future, it will be our goal to get 100 percent of our script placed there.

click to expand
Figure 2.9: You can use trace to print the current value of a variable into the Output window.
 result1 = 5 + 3; result2 = 7 - 4; result3 = 3 * 2; result4 = 7 / 3;  trace(result1);   trace(result2);   trace(result3);   trace(result4);  

In this example, you see addition, subtraction, multiplication, and division, but you might overlook another operator ”assignment. The assignment operator ( = ) is used to assign some value to a variable. It's the way you get stuff into your variables.

You can put multiple operators in the same statement. When this is done, the operations are performed from left to right.

Note  

Actually, operations are not always performed left to right. The order that operations are executed has to do with operator precedence, which is discussed in the next section.

Consider the following statement:

 result1 = 5 + 3 - 2 + 11 - 4; 

This code is perfectly valid. The operations are performed from left to right starting with the addition of 5 and 3. 2 is then subtracted, 11 is added, and finally 4 is subtracted for a final result of 13.

Now let's have another example of these operators. Consider the following script and then put it in frame1 of a new movie for testing:

 result1 = 2 * 2 + 2; result2 = 2 + 2 * 2; trace(result1); trace(result2); 

Does anything seem funny to you when you look at the output, shown in Figure 2.10?

click to expand
Figure 2.10: The output of both statements is the same because of operator precedence.

Let's think about what the script is doing. In the first line, Flash is multiplying 2 and 2 together, adding 2 to the result, and then assigning the value to result1. 2 times 2 equals 4 plus 2 equals 6, which is confirmed by the output. But now consider the second line. The operators are reversed and the first two are added. The multiplication operation comes afterward, but we know that 2 plus 2 equals 4 and 4 times 2 equals 8, so why does the output window say 6 for the second time?




Macromedia Flash MX 2004 Game Programming
Macromedia Flash MX 2004 Game Programming (Premier Press Game Development)
ISBN: 1592000363
EAN: 2147483647
Year: 2004
Pages: 161

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net