| Register
Saturday, May 17, 2008   

Mastering the Array

Created By  Satori Canton, at  7/6/2004 - 8 comments.

Click to view this author's website.

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.

Need Professional Help For Your ActionScript Project?
ActionScript.com Consulting Services provide top quality professional ActionScript consulting to businesses around the globe. If you have a professional project in need to world-class talent, tell us about your project by requesting a quote today.

Reader Comments

  1. Zoser  Replied:
    ( 7/8/2004 At 7:38 PM)

    If I wanted to get started in flash and have OOP expierence where do I start? What dev environment do I need and what books are good for my library?

  2. Todd Coulson  Replied:
    ( 7/8/2004 At 9:26 PM)

    A good friend of mine wrote a book that is similar to the Flash Bible:

  3. immagini hard  Replied:
    ( 7/14/2004 At 2:10 PM)

    ciao ciao

  4. telefono erotico  Replied:
    ( 7/14/2004 At 2:14 PM)

    ciao ciao

  5. Dallas G. Davis  Replied:
    ( 7/21/2004 At 5:46 PM)

    Looking for a network of

  6. fred  Replied:
    ( 8/13/2004 At 6:00 PM)

    I want to make sex to the action script.... oooooooooohhhhhhh yeahhhhhhhhhhhhhhh

  7. Steve  Replied:
    ( 6/7/2005 At 9:44 PM)

    that string replace method is very cool... it was exactly what I was looking for.

  8. chams nathan  Replied:
    ( 3/17/2007 At 12:11 PM)

    I'm familiar with the properties and methods of array class in flash. But i get strucked up when i try to code using it. It would be very helpful if someone could guide me with some other diffrent samples apart from our flash F1 help. Pls let me know abt the actual place of usage of this array class. if possible refer me some sites and books to make me through with array class.

    Thanks
    Chams

Login to post your comments. If you do not have an account with us please Register.
Copyright 2005 by ActionScript, Inc.   |  Privacy Statement  |  Terms Of Use  |  ActionScript Client Extranet