Writing Scripts with ActionScript > Using operators to manipulate values in expressions

Using operators to manipulate values in expressions

An expression is any statement that Flash can evaluate that will return a value. You can create an expression by combining operators and values, or by calling a function. When you write an expression in the Actions panel in Normal Mode, make sure the Expression box is checked in the Parameters panel, otherwise the field will contain the literal value of a string.

Check the Expression box next to a field to create an expression
 

Operators are characters that specify how to combine, compare, or modify the values of an expression. The elements that the operator performs on are called operands. For example, in the following statement, the + operator adds the value of a numeric literal to the value of the variable foo; foo and 3 are the operands:

foo + 3 

This topic describes general rules about common types of operators. For detailed information on each operator mentioned here, as well as special operators that don't fall into these categories, see ActionScript dictionary: Overview.


 
Operator precedence

When two or more operators are used in the same statement, some operators take precedence over others. ActionScript follows a precise hierarchy to determine which operators to execute first. For example, multiplication is always performed before addition; however, items in parentheses take precedence over multiplication. So, without parentheses, ActionScript performs the multiplication in the following example first:

total = 2 + 4 * 3;

The result is 14.

But when parentheses surround the addition operation, ActionScript performs the addition first:

total = (2 + 4) * 3;

The result is 18.

For a table of all operators and their precedence, see Operator List.


 
Operator associativity

When two or more operators share the same precedence, their associativity determines the order in which they are performed. Associativity can either be left-to-right or right-to-left. For example, the multiplication operator has an associativity of left-to-right; therefore, the following two statements are equivalent:

total = 2 * 3 * 4;
total = (2 * 3) * 4;

For a table of all operators and their associativity, see Operator List.


 
Numeric operators

Numeric operators add, subtract, multiply, divide, and perform other arithmetic operations. Parentheses and the minus sign are arithmetic operators. The following table lists ActionScript's numeric operators:
Operator Operation performed

+

Addition

*

Multiplication

/

Division

%

Modulo

-

Subtraction

++

Increment

--

Decrement



 
Comparison operators

Comparison operators compare the values of expressions and return a Boolean value (true or false). These operators are most commonly used in loops and in conditional statements. In the following example, if variable score is 100, a certain movie loads; otherwise, a different movie loads:

if (score == 100){
	loadMovie("winner.swf", 5);
} else {
	loadMovie("loser.swf", 5);
	}

The following table lists ActionScript's comparison operators:
Operator Operation performed

<

Less than

>

Greater than

<=

Less than or equal

>=

Greater than or equal



 
String operators

The + operator has a special effect when it operates on strings: it concatenates the two string operands. For example, the following statement adds:

"Congratulations," to "Donna!":

"Congratulations, " + "Donna!"

The result is "Congratulations, Donna!" If only one of the + operator's operands is a string, Flash converts the other operand to a string.

The comparison operators >, >=, <, and <= also have a special effect when operating on strings. These operators compare two strings to determine which is first in alphabetical order. The comparison operators only compare strings if both operands are strings. If only one of the operands is a string, ActionScript converts both operands to numbers and performs a numeric comparison.

Note: ActionScript's data typing in Flash 5 allows the same operators to be used on different types of data. It is no longer necessary to use the Flash 4 string operators (for example, eq, ge, and lt) unless you are exporting as a Flash 4 movie.


 
Logical operators

Logical operators compare Boolean (true and false) values and return a third Boolean value. For example, if both operands evaluate to true, the logical AND operator (&&) returns true. If one or both of the operands evaluate to true, the logical OR operator (||) returns false. Logical operators are often used in conjunction with comparison operators to determine the condition of an if action. For example, in the following script, if both expressions are true, the if action will execute:

if ((i > 10) && (_framesloaded > 50)){
	play();
}

The following table lists ActionScript's logical operators:
Operator Operation performed

&&

Logical AND

||

Logical OR

!

Logical NOT



 
Bitwise operators

Bitwise operators internally manipulate floating-point numbers to change them into 32-bit integers, which are easier to work with. The exact bitwise operation performed depends on the operator, but all bitwise operations evaluate each digit of a floating-point number separately to compute a new value.

The following table lists ActionScript's bitwise operators:
Operator Operation performed

&

Bitwise And

|

Bitwise Or

^

Bitwise Xor

~

Bitwise Not

<<

Shift left

>>

Shift right

>>>

Shift right zero fill



 
Equality and assignment operators

You can use the equality (==) operator to determine whether the values or identities of two operands are equal. This comparison returns a Boolean (true or false) value. If the operands are strings, numbers, or Boolean values, they are compared by value. If the operands are objects or arrays, they are compared by reference.

You can use the assignment (=) operator to assign a value to a variable, as in the following:

password = "Sk8tEr";

You can also use the assignment operator to assign multiple variables in the same expression. In the following statement, the value of b is assigned to the variables c, and d:

a = b = c = d;

You can also use compound assignment operators to combine operations. Compound operators perform on both operands and then assign that new value to the first operand. For example, the following two statements are equivalent:

x += 15;
x = x + 15;

The following table lists ActionScript's equality and assignment operators:
Operator Operation performed

==

Equality

!=

Inequality

=

Assignment

+=

Addition and assignment

-=

Subtraction and assignment

*=

Multiplication and assignment

%=

Modulo and assignment

/=

Division and assignment

<<=

Bitwise shift left and assignment

>>=

Bitwise shift right and assignment

>>>=

Shift right zero fill and assignment

^=

Bitwise Xor and assignment

|=

Bitwise Or and assignment

&=

Bitwise And and assignment



 
Dot and array access operators

You can use the dot operator (.) and the array access operator ([]) to access any predefined or custom ActionScript object properties, including those of a movie clip.

The dot operator uses the name of an object on its left side and the name of a property or variable on its right side. The property or variable name can't be a string or a variable that evaluates to a string; it must be an identifier. The following are examples using the dot operator:

year.month = "June";
year.month.day = 9;

The dot operator and the array access operator perform the same role, but the dot operator takes an identifier as its property and the array access operator evaluates its contents to a name and then accesses the value of that named property. For example, the following two lines of code access the same variable velocity in the movie clip rocket:

rocket.velocity;
rocket["velocity"];

You can use the array access operator to dynamically set and retrieve instance names and variables. For example, in the following code, the expression inside the [] operator is evaluated and the result of the evaluation is used as the name of the variable to be retrieved from movie clip name:

name["mc" + i ]

If you are familiar with the Flash 4 ActionScript slash syntax, you may have done the same thing using the eval function, as in the following:

eval("mc" & i);

The array access operator can also be used on the left side of an assignment statement. This allows you to dynamically set instance, variable, and object names, as in the following example:

name[index] = "Gary";

Again, this is equivalent to the following Flash 4 ActionScript slash syntax:

Set Variable: "name:" & index = "Gary"

The array access operator can also be nested with itself to simulate multidimensional arrays.

chessboard[row][column]

This is equivalent to the following slash syntax:

eval("chessboard/" & row & ":" & column)

Note: If you want to write ActionScript that is compatible with the Flash 4 Player, you can use the eval action with the add operator.