188
Syntax and Language Fundamentals
Using numeric operators
You use numeric operators to add, subtract, divide, and multiply values in ActionScript. You
can perform different kinds of arithmetic operations. One of the most common operators is
the increment operator, commonly formed as
i++
. There are more things you can do with this
operator. For more information on the increment operator, see
“Using operators to
manipulate values” on page 177
.
You can add the increment before (
preincrement
) or after (
postincrement
) an operand.
To understand numeric operators in ActionScript:
1.
Create a new Flash document.
2.
Type the following ActionScript into Frame 1 of the Timeline:
// example one
var firstScore:Number = 29;
if (++firstScore >= 30) {
// should trace
trace("Success! ++firstScore is >= 30");
}
// example two
var secondScore:Number = 29;
if (seco+ >= 30) {
// shouldn't trace
trace("Success! seco+ is >= 30");
}
3.
Select Control > Test Movie to test the ActionScript
The “Example one” code block traces, but the “Example two” code block does not. The
first example uses a preincrement (
++firstScore
) to increment and calculate
firstScore
before it’s tested against 30. Therefore,
firstScore
increments to 30 and
then tests against 30.
However, Example two uses a postincrement (
seco+
), which evaluates after the
test is performed. Therefore, 29 compares against 30, and then increments to 30 after
the evaluation.
For more information on operator precedence, see
“About operator precedence and
associativity” on page 179
.
When you load data from external sources (such as XML files, FlashVars, web services, and so
on), you need to be very careful when you work with numeric operators. Sometimes Flash
treats the numbers like strings because the SWF file isn’t aware of the number’s data type. In
this case, you could add 3 and 7 with a result of 37 because both numbers are concatenated
like strings instead of adding numerically. In this situation, you need to manually convert the
data from strings to numbers using the
Number()
function.
Содержание FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
Страница 1: ...Learning ActionScript 2 0 in Flash...
Страница 8: ...8 Contents...
Страница 18: ...18 Introduction...
Страница 30: ...30 What s New in Flash 8 ActionScript...
Страница 66: ...66 Writing and Editing ActionScript 2 0...
Страница 328: ...328 Interfaces...
Страница 350: ...350 Handling Events...
Страница 590: ...590 Creating Interaction with ActionScript...
Страница 710: ...710 Understanding Security...
Страница 730: ...730 Debugging Applications...
Страница 780: ...780 Deprecated Flash 4 operators...
Страница 830: ...830 Index...