Flash
  Home arrow Flash arrow Referencing Movie Clips in Flash MX
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
FLASH

Referencing Movie Clips in Flash MX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2007-02-08

    Table of Contents:
  • Referencing Movie Clips in Flash MX
  • Referring to Nested Instances
  • Authoring Instance References with Insert Target Path
  • Storing references to clips in data containers

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Referencing Movie Clips in Flash MX


    (Page 1 of 4 )

    In this third part of a four-part series, you will learn how to store references to clips in data containers, refer to nested instances, and more. It 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.

    Referring to the Current Instance or Movie

    We don’t always have to use an instance’s name when referring to a clip. Code attached to a frame in an instance’s timeline can refer to that instance’s properties and methods directly, without any instance name.

    For example, to set the_alphaproperty of a clip namedcloud_mc, we can place the following code on a frame in thecloud_mctimeline:

      _alpha = 60;

    Similarly, to invoke the play() method oncloud_mcfrom a frame in thecloud_mctimeline, we can simply use:

      play();

    This technique can be used on any timeline, including timelines of main movies. For example, the following two statements are synonymous if attached to a frame on the main timeline of a Flash document. The first refers to the main movie implicitly, whereas the second refers to the main movie explicitly via the global_root property:

      gotoAndStop(20);
      _root.gotoAndStop(20);

    However, not all methods can be used with an implicit reference to a movie clip. Any movie clip method that has the same name as a corresponding global function, such as duplicateMovieClip() or unloadMovie(), must be invoked with an explicit instance reference. Hence, when in doubt, use an explicit reference. We’ll have more to say about method and global function conflicts later in in this chapter under “Method Versus Global Function Overlap Issues.”

    Note that it’s always safest to use explicit references to variables or movie clips rather than using implicit references. Implicit references are ambiguous, often causing unexpected results and confusing other developers reading your code.

    Self-references with the this keyword

    When we want to refer explicitly to the current instance from a frame in its timeline or from one of its event handlers, we can use the this keyword. For example, the following statements are synonymous when attached to a frame in the timeline of our cloud_mcinstance:

      _alpha = 60;        // Implicit reference to the current timeline
      this._alpha = 60;   // Explicit reference to the current timeline

    There are three reasons to usethisto refer to a clip even when we could legitimately refer to the clip’s properties and methods directly.

    • First, explicit references are easier for other developers to read, because they make the intention of a statement unambiguous.
    • Second, when used without an explicit instance reference, certain movie clip methods are mistaken for global functions by the interpreter. If we omit thethisreference, the interpreter thinks we’re trying to invoke the analogous global function and complains that we’re missing the target movie clip parameter. To work around the problem, we usethis, as follows:

        this.duplicateMovieClip("newClouds_mc", 0); // Invoke a method on an instance

        // If we omit the this reference, we get an error
        duplicateMovieClip("newClouds_mc", 0);      // Oops! 
    • Third, usingthis, we can conveniently pass a reference to the current timeline to functions that operate on movie clips:

        // Here's a function that manipulates clips
       
      function moveClipTo (theClip, x, y) {
        
      theClip._x = x;
        
      theClip._y = y;
       }

       
      // Now let's invoke it on the current timeline
       
      moveClipTo(this, 150, 125);

      New and experienced object-oriented programmers alike, take note: the meaning ofthisinside a method is a reference not to the current timeline but to the object through which the method was invoked. If an object’s method needs to refer to a specific movie clip, a reference to that clip should be passed to the method as a parameter. See Chapter 12 for details on using the thiskeyword inside methods.

    More Flash Articles
    More By O'Reilly Media


       · This article is an excerpt from the book "ActionScript for Flash MX: the Definitive...
     

    Buy this book now. This article 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). Check it out today at your favorite bookstore. Buy this book now.

    FLASH ARTICLES

    - 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
    - Organizing Frames and Layers for Flash Anima...
    - Organizing Frames and Layers
    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    Stay green...Green IT