Flash: Instance names vs. variable names

There is some confusion as to the nature of instance names in AS3. This is because in AS2, you could use a string of actual text to create an instance name, using the createEmptyMovieClip method (if you were creating a MovieClip object from scratch) or the attachMovie method (if you were dynamically loading an instance of a MovieClip symbol from the library). In either case, the string of text that you supplied to the method for the "instance name" parameter became the instance name of that instance. Internally, Flash was taking that string of text and somehow creating a variable name for you with the same name. This variable name was available for use immediately, on the very next line of code. At the same time, the supplied string of text was assigned to the instance's _name property (note the underscore, this is AS2, remember?).

I have written about this issue before [Click Here], but to recap, in AS3 you can no longer generate variable names from a simple string like this. You can create as many variable names as you want or need, you just can't generate them from a string of text any more. In AS2, there was a popular technique where you would generate instance names in a for loop, and just append the loop counter to the string to make them unique. Click the link I just gave you above for more information (if you haven't already read it).

So why am I writing about this issue again? Well, this time, I want to delve into the nature of variables in AS3, and I thought I would begin by discussing how variables differ from instance names. Or are they the same thing? Just what is an instance name, anyway? It winds up being all very simple, after all. But let's get it straight right up front, an "instance name" is a feature of the Flash authoring tool, and it actually has no meaning in terms of programming. An instance name is just that string of text that you type into the properties panel to give a variable name to a MovieClip instance that you have created on the stage. Flash takes that value that you type into the box and, behind the scenes, creates a variable name for you. At the same time, Flash also sets the _name (AS2) or name (AS3) property for the instance to that same string of text. But it's important to understand that this property is still just a simple string, it is not a variable name that can be used in your programming statements. The fact that Flash turns this string into a variable name for you behind the scenes gives the illusion that you are using the instance name itself in your programming, but actually you are not.

But the fact that Flash does this also perpetuates a kind of feeling that might be described as "one name per object," or the idea that each object has one instance name, and that's that. I am going to show you that that's not the case, but we'll get to that in a bit.

The bottom line is that having a variable name that you can use to refer to the instance is the important part. The fact that Flash does this for you behind the scenes for your manually created stage instances is kind of a convenience, but it can also be doing you a disservice if you are coming from AS2 and don't exactly understand what's really going on. Flash automatically generates these variable names as part of creating a class for you that represents your fla's main timeline. This class is known as the document class. You can make your own document class, but if you don't, Flash automatically makes one for you.

In the publish settings for your fla, there is a very special checkbox labelled "automatically declare stage instances." This box is checked by default. If you uncheck it, and you don't write your own document class, Flash will still perform this service for you anyway as part of generating your document class. If you uncheck the setting, and you do write your own document class, you have to declare all the stage instances yourself manually in your document class. If you have classes linked to library symbols, and those symbols contain nested instances of their own, those instances will have to be declared manually, too, in the linked classes. That is, if you uncheck the box.

To get to this setting, go to File, Publish Settings, and you will get the Publish Settings dialog box. Now in the box, click the Flash tab, and on the second line where it says Script: Actionscript 3.0, click the Settings.... button to the immediate right of that:

This will lead you to the following dialog box, where the "automatically declare stage instances" checkbox can be found:

Leaving this box checked or not is totally up to you, but I'll give you a few general rules of thumb: If you write your code on the main timeline of your fla and not your own document class, leave the box checked, as unchecking it will make no difference anyway. Secondly, if you know that any given project is going to be strictly a Flash project, you might as well leave the box checked and let Flash declare your stage instances for you, even if you always write your own document class. If, however, you use Flex or can foresee that you might eventually bring your project into Flex, then you should uncheck the box and declare your stage instances manually.