Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Now we just talked about arrays. And because we just talked about arrays I want to talk to you about something else that's very closely related. And that's something called an Associative Array. When we use an array object then we're using the Array class. And the Array class gives us the opportunity to order our data using integer indexes. Integers are whole numbers. So, 0, 1, 2, 3, 4, 5, et cetera. Now an Associative Array doesn't necessarily have to be created using the Array class.
In fact, most of the time we're going to create Associative Arrays using the Object class. That's object with a capital O. Object is the base class - sort of the most fundamental class upon which all other classes are based. Typically, we'll use the Object class to create an Associative Array. What an Associative Array does is it helps you to organize your data. Instead of using an integer index, we use a string index.
And we refer to those string indices as keys. An array created using the Array class like we just talked about in the previous lesson, is useful when you want to create some structure to your data. That way you can sort your data and organize it in lots of different ways. Using an Associative Array is helpful when you want to group some related data, but they don't necessarily have any order. Instead they have particular significances. And what I mean by that is, for example, you might have some information about a company.
And you would have the employees of your company. You might have John, and Sue, and Fred. And each of these people who are employees have different titles. Maybe one's the president and one's the CEO, and one's the CTO. So they don't necessarily have a particular order. But they do have particular significance. And they're all related. So we could use an Associative Array in order to group that information. And then we could give each of the pieces of data a key in order to be able to reference it.
And the key in those cases would be president, or CEO, or CTO. So let's take a look at a code example of this. I'm going to open the Actions panel and I'm going to instantiate an Associative Array. So I am going use the var keyword, of course, because I want to declare a variable in order to store my Associative Array. And I'll call it oEmployees. And I'll define that as being an Object datatype.
And then I am going to use the Object constructor - that's object with a capital O - in a new statement. So I'll type new Object. Now, one thing I want to talk about before we move on is we have the Object with a capital O. And because ActionScript in MX 2004 is case sensitive, then if we were to use a lowercase o, that's not the same thing. When we use the lowercase o, it's just simply a term we use to describe a generic instance of a class.
But the Object with a capital O is a specific class name. And you'll notice that when I type it with a capital O, then I get code syntax coloring, and that indicates that that is a keyword. And that is the Object class. And that's differentiated from object with a lowercase o, which is a general term that we use to describe instances of classes. Whether that be an instance of the Array class or a MovieClip class, or any of the other classes that we'll discuss throughout the remaining lessons of this title.
So in order to create our simple Associative Array, we're creating an instance of the Object class. In order to add data to my Associative Array I am going to use dot syntax. There are two alternative ways that you can do this. And we're going to start with dot syntax. In order to do that, I am going to type the name of my Associative Array. Which is oEmployees. And then I use dot. And then I follow that with the key. And the key is something that I get to make up.
And in this case I'm going call one of them president. And then I use an assignment operator. And I assign to that the value of - let's say that "Sue" is the president of this particular company. And then I can add another element to my Associative Array. And let's say that "John" is the CTO of my company.
And let's add one more. And let's say that "Sarah" is the CEO of this company. So I have created an Associative Array that has three elements. And each element has a key. The keys are president, cto, and ceo. And then I have the values that I have associated with those keys of Sue, John, and Sarah. Now, if I want to be able to retrieve those values, then I can use the dot syntax in a situation such as within a trace statement.
If I wanted to output the value, then I could use, as an example, oEmployees.president. And when I test the movie, I'll see that Sue is displayed in the Output panel. Now there's another way that we can reference the elements of the array. And that is using array access notation. So let's take a look at that, and then we'll compare and contrast the benefits of each of these two techniques.
So using array access notation, we still start with the name of the Associative Array. Which is oEmployees, in this case. And then I use the square brackets in order to specify the key. And then the key is going to be specified as a string. So let's create one more element in our Associative Array. And we'll use the key of "cfo". And let's assign that a value of "Arun".
And then if I want, I can also reference that value, and we could use a trace statement to illustrate this. I can also reference the value using array access notation. So I can type oEmployees and use the square brackets. When I test the movie I will see that Arun is displayed in the Output panel. I can additionally, if I wanted, I could also reference the same element using dot syntax.
So they're interchangeable for the most part. And the same would be true if I wanted to reference the CEO using array access notation. So why would I use one over the other? It's a good question. The benefit that the array access notation has over the dot notation is that the key is specified as a string. And this becomes really valuable when we're wanting to use things that are more dynamic. For example, if our Associative Array has keys that are determined more programmatically, or if we don't know them to hardcode them at the time we're writing our code, then we want to use the array access notation because then we can use a variable as the key.
Here's an example. If I create a variable called sKey and I assign that a value of "ceo", then I can reference the element that corresponds in my Associative Array using the sKey variable inside of the square brackets. In order to get the bigger picture for this, I am going to tell you this. All objects created from all classes in ActionScripts can be treated as Associative Arrays, this becomes particularly valuable when we're working with, for instance, movie clips.
And then what happens is a lot of people often times want to know how they can reference a movie clip dynamically. And what they can do is use Array Access notation in order to reference the movie clip. And we'll get a chance to see some of that in action in the movie clip section of this title. All right. Objects and arrays. They can seem a little bit complex when you're first starting with them. But you can definitely pat yourself on the back; it's an advanced topic, so you're definitely moving along on the ActionScript path.
We're going to be using a lot of these concepts more in the later exercises, so you'll get a chance to practice with them. One thing that I need to tell you, though, is it's important that you just be willing to have an open mind and try these things. There's something to be said for "Slow and steady wins the race." That's true. But you also need to be willing to take the steps. So we're going to be walking through a lot of exercises together. I would encourage you to practice with them. If there's any sticking point, then try it again. And you'll always find the finished and commented versions of all the exercises to use for reference if there's any point that you're uncertain about.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 99334 Viewers
56 Video lessons · 112595 Viewers
71 Video lessons · 81415 Viewers
131 Video lessons · 39083 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.