Variables and Combined Assignment Operators
You'll commonly want to complete a math function and assign the resulting value back to some named object, called a variable. ActionScript makes this easier by letting you combine arithmetic and assignment operators together. Take a look at an assignment operator example:
- Create a new ActionScript 3.0 project and enter in the following code for the project:
// Assignment Operators var myValue:Number = 2; myValue = myValue + 2; trace(myValue); var myOtherValue:Number = 2; myOtherValue += 2; trace(myOtherValue);
- Run the project. You'll get the following in the Output panel:
Let's walk through the code and explain how you get this result and what role variables and combined assignment operators play.
You haven't really seen much about the var statement yet, so let's reveal a little bit more about it. You have used it in the past to create named object containers that you have then assigned MovieClip symbols to using the new statement. You can also use var to create variables; in fact, variables is what var stands for. Variables are named objects that can contain variable values.
Take a look at the second line of the assignment operators example:
var myValue:Number = 2;
The var statement is creating a variable called myValue. See that :Number after the variable name? You have to tell ActionScript what type of data your variable can hold, similar to how you did when using the function statement. In this case, you are saying that myValue will contain a number. When you create the variable, it is empty, but when you assign the numeric value 2 to it, you can refer to that value using the name myValue.
myValue = myValue + 2; trace(myValue);
On the second line above, you are accessing the myValue object and are assigning a new value to it. Notice that you are not using the var statement here, because var is only used to create a new variable. You don't need to use it again if you are referring to a variable that has already been created. Before you assign the value, you need to complete the evaluation on the right side of the assignment operator. In this case, you are taking the existing value of myValue, 2, and adding the value 2 to it. This value is then assigned back to myValue, overwriting the existing value. In the last line of the first block, you send that value to the Output panel using the trace statement, which displays 4.
This completes the analysis of the first part of the code.
Combined Assignment Operators
Take a look at the second block of code. This section of code works identically to the first block, with two exceptions. In this section, you are creating a new variable called myOtherValue:
var myOtherValue:Number = 2; myOtherValue += 2; trace(myOtherValue);
In the first line, you need to use the var statement since you have not created that variable before. You then assign the numeric value 2 to it.
On the next line, you come across the first combined assignment operator, +=. This operator is combining addition with assignment. In this case it is taking the existing value of myOtherValue and is adding 2 to it and automatically assigning it back to the myOtherValue variable. Always put the arithmetic operator before the assignment operator. You can use this shortcut with any of the basic arithmetic operators:
// All combined assignment operators var myValue:Number = 100; myValue += 50; // 100+50 = 150 myValue -= 125 // 150-125 = 25 myValue *= 3 // 25*3 = 75 myValue /= 5 // 75/5 = 15 myValue %= 4 // 15%4 = 3 trace (myValue);
Programmers often use these combined assignment operators as shortcuts since they are nice time savers. Hopefully, you'll find they are too!