Home arrow Flash arrow Organizing Movie Clips in Flash MX

Organizing Movie Clips in Flash MX

Continuing our discussion of Flash, in this article you'll learn how to import external movies and images, add identifiers to movie clips, and more. This article, the second of four parts, is excerpted from chapter 13 of the book ActionScript for Flash MX: the Definitive Guide, second edition, written by Colin Moock (O'Reilly; ISBN: 059600396X). Copyright © 2005 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 5 stars5 stars5 stars5 stars5 stars / 6
February 01, 2007
  1. · Organizing Movie Clips in Flash MX
  2. · Importing External Movies and Images
  3. · Movie and Instance Stacking Order
  4. · Stacks and Order of Execution

print this article

Organizing Movie Clips in Flash MX
(Page 1 of 4 )

Movie Clip Instance Names

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:

  1. Select the instance on stage.
  2. 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):

trace(instance1._name);         // Displays instance1

trace(instance1);              // Displays _level0.instance1

trace(String(instance1));     // Displays _level0.instance1

trace(targetPath(instance1)); // Displays _level0.instance1

trace(instance1.toString()); // Displays [object Object]

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.

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_m
                              // 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.

blog comments powered by Disqus

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials