Game Development Reference
In-Depth Information
Using increment and decrement operators
There's a slightly more efficient way to write this code. Updating values incrementally, as you did before,
is such a common and useful thing that AS3.0 has specialized operators that do the work for you.
Operators are symbols such as =, -, + and *, which perform specialized tricks with
values, such as assigning, adding, or subtracting them.
The two new operators that you'll use are called the increment and decrement operators. Update
the kjQl>qppkj?he_g and kj@ksj>qppkj?he_g event handlers so that they use these operators:
1. Modify the kjQl>qppkj?he_g and kj@ksj>qppkj?he_g event handlers so that they reflect the
changes shown following. (I haven't included the pn]_a directives, and you can remove them if
you want to, but leaving them in is just fine, too! It won't affect how the program runs.)
bqj_pekjkjQl>qppkj?he_g$arajp6IkqoaArajp%6rke`
w
dehhL]ca*_]p*u)9-17
y
bqj_pekjkj@ksj>qppkj?he_g$arajp6IkqoaArajp%6rke`
w
dehhL]ca*_]p*u'9-17
y
2. Save the I]ej*]o file and test the project.
The functionality of the program is exactly the same, but I simplified the code a bit by using the decre-
ment operator:
)9
and the increment operator:
'9
All these operators do is assign the new value back into the property on the left of the operator sign.
The -= operator subtracts the value, and the += operator adds it.
Incrementing and decrementing are a game designer's staple, so get used to using them because you'll
be seeing them a lot from now on.
 
Search Nedrilad ::




Custom Search