When we create instances, we assign them identifiers, or instance names, that allow us to refer to them later. Assigning identifiers to movie clips differs from assigning them to regular objects. When we create a typical object (not a movie clip), we must assign that object to a variable or other data container in order for the object to persist and in order for us to refer to it by name in the future. For example:
new Object(); // Object dies immediately after it's created, and // we can't refer to it because we didn't store it.
var thing = new Object(); // Object reference is stored in thing, // and can later be referred to as thing.
Movie clip instances need not be stored in variables in order for us to refer to them. Unlike typical objects, movie clip instances are accessible in ActionScript via their instance names as soon as they are created, either programmatically or in the authoring tool. The manner in which an instance gets its initial name depends on how it was created. Programmatically generated instances are named at runtime by the function that creates them. Manually created instances are normally assigned explicit instance names in the authoring tool through the Property inspector, as follows:
Select the instance on stage.
In the Property inspector, for <Instance Name>, enter the instance name.
(In Flash 5, the instance name is set via the Instance panel.) Once the instance is named in the authoring tool, it can be accessed via ActionScript using the same name. (It is good practice to add “_mc” as a suffix to the identifier name of any movie clips you create during authoring or at runtime.) For example, if there exists an instance namedball_mcon stage, we can access its properties like this:
ball_mc._y = 200;
If a manually created clip is not given an instance name, it is assigned one automatically by the Flash Player at runtime. Automatic instance names fall in the sequenceinstance1,instance2,instance3, ...instancen, but these names don’t meaningfully describe our clip’s content (and we must guess at the automatic name that was generated). For example, the first unnamed clip instance can be accessed as:
instance1._y = 200;
Because instance names assigned during either authoring or runtime are used as identifiers in ActionScript, we should always compose them according to the rules for creating a legal identifier, as described in Chapter 15. Most notably, instance names should not begin with a number or include hyphens or spaces. By convention, movie clip instance names should include the suffix “_mc”, particularly if they are going to be referenced via ActionScript at runtime.
Each clip’s instance name is stored in its built-in_nameproperty, which can be both retrieved and set. For clips defined manually during authoring, the default_nameis a string version of the original clip identifier:
trace(ball_mc._name); // Displays "ball_mc"
For programmatically defined clips, the initial value for_nameis specified by thenewName parameter passed to duplicateMovieClip() or attachMovie().
The_nameproperty is useful for debugging or displaying the name of a clip (without the fully qualified path). Note how the value returned by_namediffers from other representations of a movie clip identifier (see also the legacy_targetproperty discussed later in this chapter):
Example 13-1 uses the_nameproperty to find a particular movie clip. See “The _name property ”later in this chapter for an example showing how to use the_nameproperty to prevent an infinite loop.
Example 13-1. Finding movie clips on a timeline
// Finds all movie clips inside gameboard_mc with the word "enemy" in their name. for (var prop in gameboard_mc) { if (typeof gameboard_mc[prop] == "movieclip") { if (gameboard_mc[prop]._name.indexOf("enemy") != -1) { // Found an enemy movie clip...make it attack the player. gameboard_mc[prop].attackPlayer(); } } }
If we change an instance’s_nameproperty, all future references to the instance must use the new name. For example, if we change the value ofball_mc._name, theball_mcreference ceases to exist, and we must subsequently use the new name to refer to the instance:
ball_mc._name = "circle_mc"; // Change ball_mc's name to circle_mc trace(typeof ball_mc); // Displays "undefined" because ball_mc // no longer exists. circle_mc._x = 59; // After the name change, you must // use the clip's new name.
Therefore, you shouldn’t change a movie clip’s_nameproperty at runtime, as it is can make your code fail, or at least make it very difficult to follow.