At the bottom of the previous page, we looked at three simple data types in actionscript: Number, String and Boolean. There are, additionally, two data types (classes) which are extremely useful in Flash programming: Array and Object.
An array is a collection of pieces of data all of the same type (typically, but the compiler doesn't check for that) that are accessible by a numeric index. For example, the strings "linguine", "spaghetti", and "ziti" might all be stored in an array named pasta, and if stored in that order, the first element would be accessible as pasta[0] (the indexing always starts with 0) and the last would be pasta[2].
var pasta:Array = ["linguine", "spaghetti", "ziti"];
trace('the first element is ' + pasta[0] + ' and the last element is ' + pasta[2]);
Pasting that code into a Flash movie and ruuning it produces this in the Output panel: the first element is linguine and the last element is ziti. Like the String class, the Array class has several methods that are indispensable for manipulating arrays. Here's an example sequence of array statements, with the output from each:
| Operation | Result of trace statement |
Create a 3-element array of information sources:
var sources:Array;
sources = new Array("google","about","altavista");
trace("sources has " + sources.length + " elements");
or, the abbreviated way (note the square brackets):
var sources:Array = ["google", "about", "altavista"]; |
sources has 3 elements |
Use push method to add another element at the end of
the array
sources.push("metacrawler");
trace(sources); |
google,about,altavista,metacrawler (Note that running a trace on an array causes all elements to be dumped out as strings, one at a time, separated by commas) |
Use reverse method to reverse order of array
sources.reverse();
trace("first element is now " + sources[0]); |
first element is now metacrawler |
pop the array to remove the last element
sources.pop(); trace(sources); |
metacrawler,altavista,about |
Use splice to insert elements in the middle of the
array
sources.splice(2, 0, "atomz", "lycos"); trace(sources); |
metacrawler,altavista,atomz,lycos,about The 2 specifies that splicing will begin at the third element of the array; the 0 specifies that no elements are to be deleted (since splicing can be used either for adding elements to or deleting elements from an array); the last two parameters are array elements to insert into the array. |
Use splice to delete an element in the array
sources.splice(3, 1); trace(sources); |
metacrawler,altavista,atomz,about The 3 specifies that splicing will begin at the third element of the array; the 1 specifies that one element is to be deleted. |
Use unshift to add elements to the beginning of the
array
sources.unshift("dogpile");
trace(sources);
| dogpile,metacrawler,altavista,atomz,about |
sort the array alphabetically
sources.sort(); trace(sources); |
about,altavista,atomz,dogpile,metacrawler |
Another useful data structure is the Object data type, which can be combined with the Array type to make the equivalent of a recordset, with records and fields, as in a database.
Instances of the Object class can be assigned their own properties within the instance. This is the class from which most of Flash's built-in classes are derived (subclassed from) and so is a very useful one to become familiar with. A new variable (instance) of the Object class is created like this:
var eas2book:Object = new Object();or, the abbreviated way, with curly brackets:
var eas2book:Object = {};
or, to declare and assign a value at once:
var eas2book:Object = {
title:"Essential Actionscript 2.0",
author:"Colin Moock",
pubyr:2004
};
In that example, a new instance of the Object class named eas2book is created,
with 3 properties: title, author, and pubyr. (Each property, as noted previously,
has its own data type, though those are not explicitly declared when the object
is created).
There are two ways to access a property of an object. One way is with dot syntax:
var booktitle:String = eas2book.title;and the other way is with a string index (treating the object as what's called an associative array):
var booktitle:String = eas2book["title"];The former is more compact; the latter is useful when properties are to be accessed with a variable representation or in a loop, eg:
var field:String = "title"; var booktitle:String = eas2book[field]; trace(booktitle); // Essential Actionscript 2.0I'll repeat that because it's important to remember:
Accessing properties of an object
There are two ways to access a property in an object. One is with dot syntax:
eas2book.title;
and the other is with a string index:
eas2book["title"];
One of the most useful structures in Flash --especially when dealing with xml or database table data and list-type components like ComboBox and DataGrid-- is an array of objects. Because you can define any number of 'fields' as the properties of an object, and any number of 'records' in an array, the two make a good recordset-like combination.
The following code, for example, creates an array of objects that is sorted alphabetically by title and used as the data provider for a combobox component. The combobox (instancename bookchoices) displays a list of book titles. Each entry (element of the array) is associated with other data (the other properties of the object in that element) which could be used to populate an order form, saved in a database, or emailed to an interested party when selected from the dropdown by a site visitor:
var books:Array = [
{title:"Flash 5 Cartoons and Games", author:"Bill Turner", pubyr:2001},
{title:"Flash Web Design", author:"Hillman Curtis", pubyr:2000},
{title:"Actionscript, the Definitive Guide", author:"Colin Moock", pubyr:2001},
{title:"Programming Macromedia Flash MX", author:"Robert Penner", pubyr:2002}
];
books.sortOn("title");
bookchoices.labelField = "title";
bookchoices.dataProvider = books;
An example of the array data being displayed when an option is selected from the menu may be found on the XML page.
For another example of the advantages of using an array of objects, see the page on how to set up a group of buttons in a movie.
Discussed on this page:
array, Array methods, push, pop, splice, object, dot syntax, associative array, array of objects, recordset, dataprovider for v2 component