How variables contain information

Now we can proceed to discuss variables and how they store information. In Actionscript 3.0, there are two types of variables, "simple" and "complex." The simple variable types are also sometimes called "primitive." The simple variable types are String, Boolean, Number, int, and uint. The complex variable types are everything else: arrays, classes that come with flash, custom classes that you write yourself, third-party classes that other people write. In other words, if it isn't a simple type, it's a complex type.

I mention this difference between simple and complex types because there is also a difference in the way that variables share their information with each other, depending on which category they fall into. This difference is important to know, especially when you are creating objects from classes.

Simple variables pass information by value:

Let's create a couple of simple string variables:

var fruit1:String = "apple";
var fruit2:String = "pear";

fruit2 = fruit1;

trace(fruit1); // apple
trace(fruit2); // apple

fruit1 = "banana";

trace(fruit1); // banana
trace(fruit2); // apple

Notice that we create a string variable called fruit1 and give it a value ("apple"). On the second line, we create another variable called fruit2, and give it a value ("pear"). Next, on line 4, we set fruit2 equal to fruit1. Not surprisingly, when the values are traced, they are both the same. Next, on line 9, we change fruit1's value to something else ("banana"). Now when we trace out both values, they are different. Changing fruit1's value did not affect fruit2's value. So, obviously, the two variables are pointing to two different storage locations. When fruit2 was set equal to fruit1, fruit2 was given a copy of the information being held by fruit1. Another way of saying it is that fruit2 was assigned the value of fruit1. This is probably the sort of behavior you would expect, especially if you have been taught that variables are like containers. But this sort of behavior is only true of the simple variable types (the three Number types and the Boolean type behave this way, too, but I thought the String type made a decent enough demonstration of it). And this behavior can be thought of as being assigned by value:

Complex variables pass information by reference:

To illustrate this concept, create a new FLA file in flash. Choose the Oval tool and draw a red circle with a black stroke. This can be any size you want. Select the circle, stroke and all, and convert it to a MovieClip symbol. Name it "Circle" and check off "export for actionscript." Let the export name be "Circle" also. Once the Circle symbol is in your library, you may delete the instance you just drew (that's still on the stage).

Next, click on the first frame of the timeline. Press F9 to get the actions panel. In the actions panel, enter the following:

var circle1:Circle = new Circle();
addChild(circle1);

Press CTRL-ENTER to run. You should see an instance of your Circle MovieClip instance appear on the stage. Next, let's add to the code, creating a second variable and setting it equal to the first one, so that the whole code now looks like this:

var circle1:Circle = new Circle();
addChild(circle1);
var circle2:Circle = circle1;
circle2.x = 150;
addChild(circle2);

Here we see that a variable called circle1 is declared, and assigned a new instance of circle. Then it is added to the display list. Next, another variable is created called circle2, and this variable is set equal to circle1. circle2 is then given an x value of 150 and added to the display list. If these variables behaved like our previous fruit1 and fruit2 variables, we should see two circles on the screen, because circle2 would get a copy of circle1. But guess what? It doesn't work that way, there is still only one circle instance, so when circle2 was told to go to an x location of 150, circle1 went there too, because both names were referring to the same object! See, when circle2 was set equal to circle1, instead of circle2 getting a copy of circle1's value, it got circle1's reference instead. Here's an illustration of this type of behavior, which may be thought of as being assigned by reference:

Don't be confused that I illustrated the "new Circle()" with a rectangle. I know that a rectangle is not a circle, but that blue rectangle is meant to represent a block of memory. Of course, it doesn't even accurately depict that, I'm sure. But I'm just trying to give you a way to picture it in your mind. Whenever the "new" keyword is used, a section of memory is set aside and the new object is created and fills this block. The variable name is just a handle, or a pointer that refers to it. Without this handle or pointer, we couldn't tell the object what to do (manipulate its properties or run its methods).

What all of this means is that only the "new" keyword actually creates any objects. You can't create a new object by assignment of one variable to another, because that only passes a reference. Also, any number of references can be made to refer to the same object. Remember my earlier discussion of instance names on the previous page? Do you see now why it's not really a valid concept to believe that there is (or can be) only one instance name for each object? Flash takes the instance names you type into the properties panel and makes variable names out of them as a service to you. Variable names for objects are simply references. The same object can have any number of references pointing at it, and can be manipulated by using any of them.

Here is another case where the same object has another reference pointing to it. Maybe you are familiar with this one but just never thought of it in these terms before:

Notice that in the above example, circle1 and event.currentTarget point to the exact same object! They are one and the same. Okay, here is yet one more example:

By now I am sure you are beginning to see the pattern here. Once a reference is established it refers back to the object itself. That being the case, one reference is as good as another. So what is the object's "real" name? I'll let you be the judge, I don't think it really has a "real" name at all. Man! To keep track of all these names you need a scorecard, don't you? Well, you have one--it's called the code window.... haha!

Finally, here is one of my favorite uses of this feature. This involves the creation of a variable whose name I invariably (no pun intended, really!) wind up calling currentClip:

In this scenario, the variables circle1 and circle2 each refer to (separate) new instances of the Circle class. A third variable called currentClip is declared. Rather then being set to a new instance itself, it is instead set equal to circle1. However, later in the code, we may decide to make it equal to circle2. In this way, circle1 and circle2 can effectively take turns "being" the current clip. Bear in mind that when currentClip is set equal to one of the circle instances, for all practical purposes it becomes that instance (and no longer refers to the previous one). Think of the currentClip variable like a chameleon that can take on the identity of the other movie clips, only literally! This is a kind of polymorphism that can be very powerful indeed!