MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH Manual page 188

Learning actionscript 2.0 in flash
Table of Contents

Advertisement

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
operator. For more information on the increment operator, see
manipulate values" on page
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 (secondScore++ >= 30) {
// shouldn't trace
trace("Success! secondScore++ 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 (
before it's tested against 30. Therefore,
firstScore
then tests against 30.
However, Example two uses a postincrement (
test is performed. Therefore, 29 compares against 30, and then increments to 30 after
the evaluation.
For more information on operator precedence, see
associativity" on page
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
188
Syntax and Language Fundamentals
177.
++firstScore
179.
Number()
. There are more things you can do with this
i++
"Using operators to
) to increment and calculate
firstScore
), which evaluates after the
secondScore++
"About operator precedence and
function.
increments to 30 and

Hide quick links:

Advertisement

Table of Contents
loading
Need help?

Need help?

Do you have a question about the FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH and is the answer not in the manual?

Subscribe to Our Youtube Channel

This manual is also suitable for:

Flash 8

Table of Contents