EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 90

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
90 Data and Data Types
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
on page 163 and About using dot syntax to target an instance” on page 118.
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.
About operators and variables
You might wonder about the mathematical symbols in your code. These symbols are called
operators in ActionScript. Operators calculate a new value from one or more values, and you
use an operator to assign a value to a variable in your code. You use the equality (
=) operator
to assign a value to a variable:
var username:String = "Gus";
Another example is the addition (+) operator, which you use to add two or more numeric
values to produce a new value. If you use the + operator on two or more string values, the
strings will be concatenated. The values that operators manipulate are called operands.
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.

Table of Contents

Related product manuals