Friday, October 29, 2004

Mastering the Array

Mastering the use of Arrays is important in any programming language. But it's especially important in ActionScript. The skills you learn by commanding arrays can easily be applied to just about every object in Flash.


Everything in Flash is an Object. And, an Object can also be treated as an associative array. For example, we can use dot-syntax to access an object's property or method as in: myObject.property. Or, we can use array brackets as in: myObject[“property”].


When I'm trying to learn about a new object, an easy way to see what properties and methods it exposes is to loop through the object and trace out the properties and methods it contains. I use this simple script to explore an object:


exploreObject = function (o) {

for (var i in o) {

trace (i + ": " + o[i]);

}

}

exploreObject(button_mc);


In this example, you could take a button component and place it on the stage. Give it an instance name of button_mc, and run the script. This will trace out all of the properties and methods of a button component. You can then look through it to find the property or method you are looking for. Sometimes you'll find additional objects as properties of the first object you are exploring. You can easily loop through them by passing them into the same function as in: exploreObject(button_mc.textStyle);


Strings are another object in Flash that can be thought of as an array. Although they are also an Object they can also be thought of as an array of characters. Strings and Arrays both have length properties, allowing you to loop through them and evaluate their contents. You can easily convert a string to an array and vice-versa. This trick is useful if you are loading external data into flash. You could load a delimited list of data into Flash as a string, and then split it into a data array by the delimiter, as in: var myArray = myString.split(“,”);


Being able to easily split strings into arrays and then back into strings also makes for a simple and elegant way of creating a String replace function. If we write this using the String.prototype, all Strings every in our Flash movie will be able to use this replace function.


String.prototype.replace = function(replaceString, withString) {

var myArray = this.split(replaceString);

return(myArray.join(withString));

};


When we call this function, we'll pass two parameters into it. The first is the substring we want to replace and the second is the string we want to put in its place. When this function is called, it uses the String.split() method to break the string into an Array, called myArray. The split method uses a string that is passed into it, in this case our replaceString, and everywhere this string is found, it breaks the original string into a new element in the array, removing the delimiter.


The Array.join() method does the same thing, but in reverse. It takes each item in the array, converts it to a string and inserts a specified delimiter, in this case withString, in between each array element, creating a single String. Here is an example of how it is used:


var s = "The red dog ate the red frog";

trace(s.replace("red", "green"));


These are two very simple examples, but they illustrate the power of understanding how to manipulate objects as if they were arrays. In my next tutorial, we'll build further on Arrays, exploring multi-dimensional arrays and ways that we can use them to store complex data.