About assigning values

You can define a value as the current contents of a variable. The value can be a strings, numbers, arrays, objects, XML, dates, or even custom classes that you create. Remember, you declare a variable in Flash using the var keyword. When you declare the variable, you also assign a data type to the variable. You can also assign a value to a variable, as long as the value matches the data type you assign to the variable.

The following example shows how you might create a variable called catName:

var catName:String;

After you declare the variable, you can assign a value to it. You might follow the previous line of ActionScript with this line:

catName = "Pirate Eye";

NOTE

 

Because Pirate Eye is a string, the value needs to be enclosed in straight quotes (quotation marks).

This example assigns the value of Pirate Eye to the catName variable. When you declare the variable, you can also assign a value to it instead of assigning it afterwards (as in the previous examples). You could set the catName variable when you declare it, as shown in the following example:

var catName:String = "Pirate Eye";

If you want to display the value of the catName variable in the test environment, you can use the trace() statement. This statement sends the value to the Output panel. You can trace the value of the catName variable and see that the actual value doesn't include the quotation marks by using the following ActionScript:

var catName:String = "Pirate Eye";
trace(catName); // Pirate Eye

Remember that the value you assign must match the data type that you assign to it (in this case, String). If you later try to assign a number to the catName variable, such as catName = 10, you will see the following error in the Output panel when you test the SWF file:

Type mismatch in assignment statement: found Number where String is required.

This error tells you that you attempted to set the wrong data type to a specified variable.

When you assign a numeric value to a variable, the quotation marks aren't necessary, as shown in the following code:

var numWrinkles:Number = 55;

If you want to change the value of numWrinkles later in your code, you can assign a new value using the following ActionScript:

numWrinkles = 60;

When you reassign a value to an existing variable, you don't need to use the var keyword or define the variable's data type (in this case, :Number).

If the value is numeric or Boolean (true or false), the value doesn't use straight quotes (quotation marks). Examples of numeric and Boolean values are shown in the following snippet:

var age:Number = 38;
var married:Boolean = true;
var hasChildren:Boolean = false;

In the previous example, the variable age contains an integer (nondecimal) value, although you could also use a decimal or floating-point value such as 38.4. Boolean variables (such as married or hasChildren) have only two possible values, true or false.

If you want to create an array and assign values to it, the format is slightly different, as shown in the following code:

var childrenArr:Array = new Array("Pylon", "Smithers", "Gil");

There is an alternative (shorthand) syntax for creating an array using array access operators, which use the bracket ([]) punctuators. You can rewrite the previous example as follows:

var childrenArr:Array = ["Pylon", "Smithers", "Gil"];

For more information on creating arrays and the array access operators, see About arrays and About using dot syntax to target an instance.

Similarly, you can create a new object called myObj. You can use either of the following ways to create a new object. The first (and longer) way to code an array is as follows:

var myObj:Object = new Object();
myObj.firstName = "Steve";
myObj.age = 50;
myObj.childrenArr = new Array("Mike", "Robbie", "Chip");

The second, shorthand way you can code the myObj array is as follows:

var myObj:Object = {firstName:"Steve", age:50, childrenArr:["Mike", "Robbie", "Chip"]};

As you see in this example, using the shorthand method can save a lot of typing and time, especially when you define instances of objects. It is important to be familiar with this alternate syntax because you will encounter it if you work in teams or when you work with third-party ActionScript code that you find, for example, on the Internet or in books.

NOTE

 

Not all variables need to be explicitly defined. Some variables are created by Flash automatically for you. For example, to find the dimensions of the Stage, you could use the values of the following two predefined values: Stage.width and Stage.height.


Flash CS3


 

Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000657.html