Working with Variables in Flash: Numeric operators
Difficulty: Beginner
Now that you understand the basic concept of variables, let's manipulate them with numeric operators.
You can manipulate variables with actionscript using numeric operators such as: multiplication or subtraction. Here are some common cases of variable manipulation with numeric operators.
Variable addition
number1 = 5; // declare variable number1
number2 = 5; // declare variable number2
total_number = number1 + number2; // put the sum of number1 and number2 into a variable called total_number
trace(total_number); // display the value of the variable total_number in the output window
It is also possible to join strings together using the "+" operator:
firstName = "John";
lastName = "Doe";
longName = firstName +" "+ lastName
trace(longName); // displays: John Doe
Variable subtraction
number1 = 5;
number2 = 3;
total_number = number1 - number2;
trace(total_number); // displays 2
Variable multiplication
number1 = 2;
number2 = 10;
total_number = number1 * number2;
trace(total_number); // displays 20
Variable division
number1 = 10;
number2 = 2;
total_number = number1 / number2;
trace(total_number); // displays 5
Shorthands
variable1 += 5; // shorthand for variable1 = variable1 + 5
variable1 -= 5; // shorthand for variable1 = variable1 - 5
variable1 /= 5; // shorthand for variable1 = variable1 / 5
variable1 *= 5; // shorthand for variable1 = variable1 * 5
Next Flash Tutorial > Working with variables part 2
|