LEARNING TO USE ARRAYS IN ACTIONSCRIPT 3.0

Arrays are extraordinarily versatile components of all full-featured computer languages, including ActionScript 3.0, the "object-oriented programming" language used by Flash. A major function of arrays is providing a program with many of the functions of a database program. This page provides: glosses to example Flash movies that use arrays, links to those movies in action, and links to the source files for those movies. The ActionScript code, visible in the source files for each of the example movies, contains detailed explanatory comments. Please feel free to download the source files and explore.

Note: a single array may be thought of as a variable with multiple, ordered data, like a grocery list. Each datum is assigned an index with the first position being 0, the second being 1, and so on. So groceryArray[bagels, milk, pickles] has "pickles" at index position 2.

Hover 1 (hover1.fla): when the mouse hovers over a nation's flag, the flag grows and information, such as its name, is drawn from an array and displayed in a text box.

Note: the nation flags in these example movies should be movie clips so that they can be targeted by ActionScript statements.

Hover 2 (hover2.fla): same as Hover 1, but information is drawn from parallel arrays, such as nation name and population, and then displayed in the text box using the appendText method.

Scroll 1 (scroll1.fla): clicking up- and down-buttons moves through the flag thumbnails in order and causes a corresponding text display as in Hover 2; this is a single-frame fla.

Scroll 2 (scroll2.fla): clicking right- and left-buttons in this multi-frame fla reveals the flag thumbnails in order at labeled keyframes and causes a corresponding text display as in Hover 2; this is one possible template for a sequential, PowerPoint-style slide show with the added power and flexibility of Flash enriching the usual PowerPoint functionality.

Random 1 (random1.fla): clicking a single button generates a display, as in Hover 2, of data drawn from a random index within the array; because this single-frame fla displays randomly chosen movie clips [the flags], this is one possible template for presenting questions in an interactive quiz.

Random 2 (random2.fla): clicking a single button generates a display of a movie clip that is referenced by a random index in an array pointing to labeled frames in this multi-frame fla, as in Scroll 2; the movie clips could contain much more than a blinking image of a flag and thus this fla is one possible template for presenting questions in an interactive quiz.

Note: the idea of using an array to contain a series of pointers, in this case of Random 2 to labeled frames each displaying a movie clip, rather than the array containing the data that are themselves to be displayed, is a conceptual bridge to using multi-dimensional arrays. (Multi-dimensional arrays are explained in a note below.)

Search (search.fla): entering a number in an input field, with appropriate input validation controls, returns the common names of nations with at least that population in millions; entering the common name of a nation in a different input field results either in a display as in Hover 2 or an error message. The first search button demonstrates retrieving multiple data from a single array; the second search button demonstrates retrieving related data from multiple arrays.

FunWithArrays (funwitharrays.fla): a highly annotated example of a data resource that offers multiple ways of displaying and searching including searching on multiple criteria at the same time.

ExternalArray (externalarray.fla + words.txt): a highly annotated example of a simple dictionary (word finding) application that populates its array from an external .txt file (words.txt) in the same folder, thus making possible revision of database-like data storage for a Flash movie without having to edit the Flash movie itself.

Note: because of the way Flash handles arrays, Flash can be made to simulate the functionality of database programs but cannot function as a true database program. To understand this limitation in Flash requires understanding the concept of array dimensionality.
A grocery list containing only items of unspecified type is a one-dimensional array, for example, grocery1Array[bagels, milk, pickles]. A grocery list containing purchase items identified by type is a two-dimensional array, for example, grocery2Array[food, bagels; food, milk; food, pickles; housewares, light bulbs; housewares, detergent]. In general, a one-dimensional array can be thought of as arrayOne[i] and a two-dimensional array as arrayTwo[i,j].
A three-dimensional array would be able to hold purchase type, purchase quantity, and purchase item, for example grocery3Array[food, 3, bagel; housewares, 2, light bulb].
In general, database programs can be thought of as using arrays of at least four dimensions, arrayFour[i,j,k,l] where i is a data type, j is a field name, k is a value, and l is a record identifier. For example, one record in a driver's license database might be driverLicenseArray[String, firstName, John, 354; String, lastName, Smith, 354; Date, birthDate, 19750315, 354; Date, expirationDate, 20160315, 354; String, licenseNumber, 9823645, 354]. (In fact, in working with database programs, record identifiers may be presented to the user as merely another data type, which they are, but the program handles them in a special way, always associating each component of a data-type/field-name/value triplet with a single and unchangeable record identifier regardless of whether or not the record identifiers are explicitly visible to the user as i.d. numbers.)
It is very useful to identify values by field name. For example, "Washington" as a last name is different, for the purposes of the driver's license bureau, from "Washington" as part of a driver's home address.
It is very useful to identify values not only by field name but by data type since different data types are susceptible to different operations; for example, subtracting dates (like subtracting birthdate from today to find age) is a different kind of operation from alphabetizing names. It makes no sense to subtract names from each other or, in most cases, to alphabetize dates. It also makes no sense to average driver license numbers, an operation that driverLicenseArray is designed to prohibit by specifying that the licenseNumber "number" is of data type String.
Multidimensional arrays, essential to true database programs, are also called nested arrays because one can think of each of the parts of an entry in a multidimensional array as being drawn from separate one-dimensional arrays; for example arrayMultiD[dataTypeArray, fieldNameArray, valueArray] could be a multidimensional array where dataTypeArray and fieldNameArray each might have only a small, limited set of entries while valueArray could have an ever expanding set of entries.
ActionScript 3.0 does not handle multidimensional (nested) arrays and therefore cannot function as a true database program. However, as the Flash array examples here show, particularly Search and FunWithArrays, by using multiple one-dimensional arrays, ActionScript can be made to simulate the functionality of multidimensional arrays and thus produce Flash movies with much of the functionality of a database.

(Introduction to Flash)

Copyright © 2009 Eric S. Rabkin
Last modified Friday, 04-Sep-2009 18:13:09 EDT .