Recipe 1.7. Using
Mathematical Operators
Problem
You want to
modify something over time, such as the rotation or position of a
sprite.
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 sprite to a new position that is 10 pixels to the
right of its current position.
In an assignment statementany 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 some number:
// Add 6 to the current value of quantity, and assign that new
// value back to quantity. For example, if quantity was 4, this
// statement sets it to 10.
quantity = quantity + 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 quantity:
quantity = quantity + 6;
quantity += 6;
These statements both subtract 6 from the
existing value of quantity:
quantity = quantity - 6;
quantity -= 6;
These statements both multiple quantity
by factor:
quantity = quantity * factor;
quantity *= factor;
These statements both divide quantity
by factor:
quantity = quantity / factor;
quantity /= factor;
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.
This statement adds 1 to quantity:
quantity++;
and has the same effect as either of these
statements:
quantity = quantity + 1;
quantity += 1;
This statement subtracts 1 from
quantity:
quantity --;
and has the same effect as either of these
statements:
quantity = quantity 1;
quantity -= 1;
You can use the increment and decrement
operators (-- and
++) either before or
after the variable or property they operate upon. 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 you're using postfix operators, the value is
modified after the remainder of the statement has executed. Note
how the first example increments quantity after displaying
its value, whereas the second example increments quantity
before displaying its value:
var quantity:Number = 5;
trace(quantity++); // Displays: 5
trace(quantity); // Displays: 6
var quantity:Number = 5;
trace(++quantity); // Displays: 6
trace(quantity); // Displays: 6
Getting back to the original problem, you can
use these operators to modify a property over time. This example
causes the specified sprite to rotate by five degrees each time the
method is called:
private function onEnterFrame(event:Event) {
_sprite.rotation += 5;
}
Note that in ActionScript 3.0, you would have to
add an event listener to the enterFrame event and set this
method as the event handler for this to work properly. See
Recipe
1.5 for information on how to handle the enterFrame event.
See Also
Recipe
1.5
|