1.1. Using Mathematical Operators
Problem
You want to modify something over time, such as the rotation or position of a movie clip.
Solution
Use the compound assignment operators to change a variable or property in increments. Or, if incrementing or decrementing by one, use the prefix or postfix increment or decrement operators.
Discussion
Often, you’ll want the new value of a variable or property to depend on the previous value. For example, you might want to move a movie clip to a new position that is 10 pixels to the right of its current position.
In an assignment statement—any statement using the assignment operator (an equals sign)—the expression to the right of the equals sign is evaluated and the result is stored in the variable or property on the left side. Therefore, you can modify the value of a variable in an expression on the right side of the equation and assign that new value to the very same variable on the left side of the equation.
Although the following may look strange to those who remember basic algebra, it is very common for a variable to be set equal to itself plus a number:
// Add 6 to the current value ofmyNumand assign that new value back tomyNum. For // example, ifmyNumwas 3, this statement sets it to 9. myNum = myNum + 6;
However, when performing mathematical operations, it is often more
convenient to use one of the compound assignment
operators, which combine a mathematical operator with the
assignment operator. The +=,
-=, *=, and
/= operators are the ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access