13.08.2012 Views

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

ACTIONSCRIPT 3 Developer’s Guide en

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>ACTIONSCRIPT</strong> 3.0 DEVELOPER’S GUIDE<br />

Working with arrays<br />

You can also create arrays with Array literals. An Array literal can be assigned directly to an array variable, as shown<br />

in the following example:<br />

var names:Array = ["John", "Jane", "David"];<br />

Creating a Vector instance<br />

Flash Player 10 and later, Adobe AIR 1.5 and later<br />

You create a Vector instance by calling the Vector.() constructor. You can also create a Vector by calling the<br />

Vector.() global function. That function converts a specified object to a Vector instance. In Flash<br />

Professional CS5 and later, Flash Builder 4 and later, and Flex 4 and later, you can also create a vector instance by using<br />

Vector literal syntax.<br />

Any time you declare a Vector variable (or similarly, a Vector method parameter or method return type) you specify<br />

the base type of the Vector variable. You also specify the base type wh<strong>en</strong> you create a Vector instance by calling the<br />

Vector.() constructor. Put another way, any time you use the term Vector in ActionScript, it is accompanied by<br />

a base type.<br />

You specify the Vector’s base type using type parameter syntax. The type parameter immediately follows the word<br />

Vector in the code. It consists of a dot (.), th<strong>en</strong> the base class name surrounded by angle brackets (), as shown in<br />

this example:<br />

var v:Vector.;<br />

v = new Vector.();<br />

In the first line of the example, the variable v is declared as a Vector. instance. In other words, it repres<strong>en</strong>ts<br />

an indexed array that can only hold String instances. The second line calls the Vector() constructor to create an<br />

instance of the same Vector type (that is, a Vector whose elem<strong>en</strong>ts are all String objects). It assigns that object to v.<br />

Using the Vector.() constructor<br />

If you use the Vector.() constructor without any argum<strong>en</strong>ts, it creates an empty Vector instance. You can test<br />

that a Vector is empty by checking its l<strong>en</strong>gth property. For example, the following code calls the Vector.()<br />

constructor with no argum<strong>en</strong>ts:<br />

var names:Vector. = new Vector.();<br />

trace(names.l<strong>en</strong>gth); // output: 0<br />

If you know ahead of time how many elem<strong>en</strong>ts a Vector initially needs, you can pre-define the number of elem<strong>en</strong>ts in<br />

the Vector. To create a Vector with a certain number of elem<strong>en</strong>ts, pass the number of elem<strong>en</strong>ts as the first parameter<br />

(the l<strong>en</strong>gth parameter). Because Vector elem<strong>en</strong>ts can’t be empty, the elem<strong>en</strong>ts are filled with instances of the base<br />

type. If the base type is a refer<strong>en</strong>ce type that allows null values, the elem<strong>en</strong>ts all contain null. Otherwise, the elem<strong>en</strong>ts<br />

all contain the default value for the class. For example, a uint variable can’t be null. Consequ<strong>en</strong>tly, in the following<br />

code listing the Vector named ages is created with sev<strong>en</strong> elem<strong>en</strong>ts, each containing the value 0:<br />

var ages:Vector. = new Vector.(7);<br />

trace(ages); // output: 0,0,0,0,0,0,0<br />

Finally, using the Vector.() constructor you can also create a fixed-l<strong>en</strong>gth Vector by passing true for the second<br />

parameter (the fixed parameter). In that case the Vector is created with the specified number of elem<strong>en</strong>ts and the<br />

number of elem<strong>en</strong>ts can’t be changed. Note, however, that you can still change the values of the elem<strong>en</strong>ts of a fixedl<strong>en</strong>gth<br />

Vector.<br />

Last updated 6/6/2012<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!