Internet / Software Applications

Macromedia Flash MX 2004 ActionScript Programming Tutorial

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you will learn about:

  • Arrays
  • Building arrays
  • Manipulating arrays
  • Nested arrays
  • Associative arrays

Functions

A function is a code module that you can call from anywhere in the movie to perform a set of actions. Functions let you automate repetitive tasks by allowing you to write the code once and then execute it whenever you need to. The syntax for creating a function is fairly simple:

function getPrice(baseprice, tax) {

price = (baseprice * tax) + baseprice;

}

This function, called getPrice, calculates a price by multiplying the values of the two variables baseprice and tax and then adding the result to the value of baseprice. To call the function from elsewhere in the movie, you simply type the name of the function (getPrice) and the parameters passed to the function—the values of the variables you included:

getPrice(50, .08);

Here, getPrice would return the value 54, derived from the calculation: (50 * .08) + 50.

Like movie clip instances, functions use absolute and relative paths, so you need to target them appropriately. For example, if the function is contained in the main timeline, you’d call it using: _root.getPrice(50, .08). If the function is contained in a movie clip instance called myMovie_mc, on the main timeline, you’d call it using: _root.myMovie_mc.getPrice(50, .08)

Remember that it’s a good idea in ActionScript 2.0 to explicitly assign data types to variables. To do this in the function we’ve been using, you’d type the following:

function getPrice(baseprice:Number, tax:Number) {

price = (baseprice * tax) + baseprice;

}

Later in the tutorial, we’ll explore functions by using some of ActionScript’s built-in functions and creating our own functions to program our game.

Arrays

Arrays store lists of related information within a single variable. For example, a firstName array might store a list of first names, and a lastName array might store a list of last names. This is much more efficient than declaring a new firstName and lastName variable (such as firstName1, lastName1, firstName2, lastName2, etc.) for each name you need to store.

Items in an array are accessed by their index position, with the first item in the array in position 0. You can replace the value of an item in the array by assigning it a new value, just as you would with an ordinary variable, and you can add new items to the array by adding index positions. In addition, you can give items in an array a unique key to enable direct access to them.

Building Arrays

The basic structure of an array is as follows:

var fName = new Array();

fName[0] = “Anne”;

fName[1] = “Jean”;

fName[2] = “Jane”;

The first line in the code uses the Array constructor to create a new, empty array called fName. The subsequent lines assign values to the first three index positions in the array, beginning with 0.

To access these values, you’d use them as you would an ordinary variable, but you’d include the index position:

myTextField.text = fName[2];

This displays “Jane” in the myTextField text field.

There are a couple of shortcuts you can take when creating arrays. You can specify the values inside an array by including them in the parentheses:

var fName = new Array(“Anne”, “Jean”, “Jane”);

Instead of creating an empty array that you then assign data to, this creates an array with 3 items already in it. The following does the same thing:

var fName = [“Anne”, “Jean”, “Jane”];

The ability to create arrays with an unlimited number of items is one of the advantages you’ll find in ActionScript as opposed to other programming languages. Arrays aren’t limited to a specified number of items. You can, however, still specify the number of items in an array by placing the number inside the parentheses:

var fName = new Array(10);

fName[0] = “Anne”;

fName[1] = “Jean”;

fName[2] = “Jane”;

This array still has 7 empty “slots” where items can be added.

Another advantage ActionScript offers with respect to arrays is the ability to use multiple data types. For example, you can create an array that stores both strings and numbers:

var UserInfo = new Array();

UserInfo[0] = “Anne”;

UserInfo[1] = “Smith”;

UserInfo[2] = 30;

You can also create an array that stores variables:

var UserInfo = new Array(fName, lName);

This code actually assigns the value of the variable fName to the first item in the UserInfo array, so UserInfo[0] is equal to fName. The value of the variable lName is assigned to the second item in the array—UserInfo[1]. You could add a third item to the array whose value is equal to another variable, age:

UserInfo[2] = age;

Manipulating Arrays

Adding an item to an array is just like assigning a value to a variable. For example, the following code creates an array called fName with 10 slots, 3 of which are filled with the values “Anne”, “Jean”, and “Jane”:

var fName = new Array(10);

fName[0] = “Anne”;

fName[1] = “Jean”;

fName[2] = “Jane”;

To add another item to the array, you’d simply add a line that specifies the index position of the new item:

fName[3] = “Margaret”;

You can do the same thing when you’ve constructed empty arrays and arrays containing specified items:

var fName = new Array();

fName[0] = “Anne”;

fName[1] = “Jean”;

fName[2] = “Jane”;

fName[3] = “Margaret”;

The code above accomplishes the same thing as the following code:

var fName = new Array(“Anne”, “Jean”, “Jane”);

fName[3] = “Margaret”;

To replace the value of an item in an array, simply specify the index position and the new value:

fName[1] = “Emma”;

This line replaces the value of fName[1] (“Jean”) with “Emma”.

You can use operators to manipulate the values of arrays just like ordinary variables, as well. You can add the values, multiply them, divide them, concatenate them, and so on. The only difference is that because an array contains multiple items, you must specify the index position of the item, or value, being manipulated.

In ActionScript, an array is actually an instance of the Array object, which means it has methods that can be accessed like any other object. The push, pop, unshift, and shift methods add and remove data in a manner like the way stacks and queues function.

The push method adds items to the end of an array, allowing you to use the array like a stack:

fName.push(“Jennifer”);

This adds the item “Jennifer” to the end of the list in the fName array. The pop method removes it:

fName.pop();

This removes the last item from the list.

To add an item to the beginning of the list, use the unshift method:

fName.unshift(“Jennifer”);

“Jennifer” will now appear in index position 0, and the other items are moved down in the list. To remove the first item in the list, use shift:

fName.shift();

In addition, you can insert items into the middle of the list using the splice method, in which you specify the new items and the position where they should be inserted:

fName.splice(2, 0, “Allison”, “Andrea”);

This inserts “Allison” into index position 2 and “Andrea” into the next index position, which would be 3. The second parameter specifies how many elements should be deleted from the array; the 0 specifies that none should be deleted, so the existing elements will just be moved down in the list.

Another handy method is reverse, which reverses the order of the items in the array:

var fName = new Array();

fName[0] = “Anne”;

fName[1] = “Jean”;

fName[2] = “Jane”;

fName.reverse();

The output of the last line would be:

Jane

Jean

Anne

The sort method sorts items alphabetically:

var fName = new Array(“Anne”, “Jean”, “Jane”);

fName.sort();

The output would be:

Anne

Jane

Jean

The sort method is alphanumeric, so in order to sort numbers, you’d have to include a function that tests whether the value of one number in the array is greater than another:

function sortArNums(a, b) {

return (a > b);

}

myArray = new Array(22, 14, 5, 61, 8, 7);

myArray.sort(sortArNums);

Nested Arrays

Arrays can be nested inside each other to create multi-dimensional arrays. There are a couple of ways to do this. The first constructs the individual arrays, and then creates an array containing them:

var fName = new Array(“Anne”, “Jean”, “Jane”);

var lName = new Array(“Smith”, “Williams”, “Johnson”);

var ContactInfro = new Array(fName, lName);

The other way is to specify all the individual elements at once:

var ContactInfro =    [        [“Anne”, “Jean”, “Jane”],

[“Smith”, “Williams”, “Johnson”]

];

Each array in the nested array is contained in brackets, and the items are separated by commas. Altogether, the arrays appear within another set of brackets.

To access an item in a nested array, you have to specify both the index position of the nested array and the index position of the item within it:

array_name[nested_array_position][item_position]

The name “Johnson” would be accessed using: ContactInfo[1][2]. 1 refers to the second array in the ContactInfo array, and 2 refers to the third index position of the second array.

Associative Arrays

An associative array, also referred to as a dictionary, uses unique keys to let you access a specific item in the array without having to loop through the list to locate the item:

var UserInfo = new Array();

UserInfo[“fName”] = “Anne”;

UserInfo.lName = “Smith”;

The last two lines accomplish the same thing—they assign a name to the item in the array. This name functions as the item’s unique key. You can access the item using the same syntax:

var message = “Welcome, ” + UserInfo[“fName”];

Or:

var message = “Welcome, ” + UserInfo.fName;

Macromedia Flash MX 2004 ActionScript 2.0 Tutorial and Free Online Training Course

In this section, you learned about:

  • Arrays
  • Building arrays
  • Manipulating arrays
  • Nested arrays
  • Associative arrays