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 of myNum and assign that new value back to myNum. For
// example, if myNum was 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 most prevalent compound assignment operators. When you use one of these compound assignment operators, the value on the right side of the assignment operator is added to, subtracted from, multiplied by, or divided into the value of the variable on the left, and the new value is assigned to the same variable. The following are a few examples of equivalent statements.

These statements both add 6 to the existing value of myNum:

myNum = myNum + 6;
myNum += 6;

These statements both subtract 6 from the existing value of myNum:

myNum = myNum - 6;
myNum -= 6;

These statements both multiply myNum by anotherNum:

myNum = myNum * anotherNum;
myNum *= anotherNum;

These statements both divide myNum by anotherNum:

myNum = myNum / anotherNum;
myNum /= anotherNum;

There should be no space between the two symbols that make up a compound assignment operator.

Additionally, if you are incrementing or decrementing a variable by 1, you can use the increment or decrement operators (-- and ++).

This statement adds 1 to myNum:

myNum++;

and has the same effect as either of these statements:

myNum = myNum + 1;
myNum += 1;

This statement subtracts 1 from myNum:

myNum--;

and has the same effect as either of these statements:

myNum = myNum - 1;
myNum -= 1;

You can use the increment and decrement operators before or after the variable or property on which they operate . If used before the operand, they are called prefix operators. If used after the operand, they are called postfix operators. The prefix and postfix operators modify the operand in the same way but at different times. In some circumstances, there is no net difference in their operation, but the distinction is still important in many cases. When using prefix operators, the value is modified before the remainder of the statement or expression is evaluated. And if using postfix operators, the value is modified after the remainder of the statement has executed. Note how the first example increments myNum after displaying its value, whereas the second example increments myNum before displaying its value:

myNum = 5;
trace(myNum++);  // Displays: 5
trace(myNum);    // Displays: 6

myNum = 5;
trace(++myNum);  // Displays: 6
trace(myNum);    // Displays: 6

Getting back to our original problem, you can use mathematical operators to modify a property over time. This example causes the specified movie clip to rotate by 5 degrees for each tick of the frame rate:

myClip_mc.onEnterFrame = function (  ) {
  this._rotation += 5;
};

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.