Flash
  Home arrow Flash arrow Fun Things to Do with Movie Clips in Flash...
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  
Dedicated Servers  
Actuate Whitepapers 
Moblin 
IBM® developerWorks 
Sun Developer Network 
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

Fun Things to Do with Movie Clips in Flash MX
By: O'Reilly Media
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 3 stars3 stars3 stars3 stars3 stars / 20
    2007-02-15

    Table of Contents:
  • Fun Things to Do with Movie Clips in Flash MX
  • Method Versus Global Function Overlap Issues
  • Drawing in a Movie Clip at Runtime
  • Using Movie Clips as Buttons
  • Input Focus and Movie Clips
  • Building a Clock with Clips

  • 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

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Fun Things to Do with Movie Clips in Flash MX


    (Page 1 of 6 )

    In this final part of a four-part series, you will learn how to remove clip instances, draw in a movie clip at run-time, 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.

    Removing Clip Instances and Main Movies

    We’ve seen how to create and refer to movie clips; now let’s see how to turn them into so many recycled electrons (in other words, blow ’em away).

    The manner in which we create an instance or a movie determines the technique we use to remove that instance or movie later. We can remove movies and instances explicitly using unloadMovie() and removeMovieClip(). Additionally, we can evict a clip implicitly by using loadMovie(), attachMovie(), or duplicateMovieClip() to place a new clip in its stead. Let’s look at these techniques individually.

    Using unloadMovie() with Instances and Levels

    The built-in unloadMovie() function can remove any clip instance or main movie—both those created manually and those created via loadMovie(), duplicateMovieClip(), and attachMovie(). It can be invoked either as a global function or as a instance-level method:

      unloadMovie(clipOrLevel);   // Global function
      clipOrLevel.unloadMovie();  // Method

    In the global function form of unloadMovie(),clipOrLevel is a string indicating the path to the clip or level to unload. Because movie clips are converted to paths when used as strings,clipOrLevel can also be a movie clip reference. In the method form of unloadMovie(),clipOrLevel must be a reference to a movie clip object. The exact behavior of unloadMovie() varies according to whether it is used on a level or an instance.

    Using unloadMovie() with levels

    When applied to a level in the document stack (e.g., _level0, _level1, or _level2), unloadMovie() completely removes the target level and the movie that the level contains. Subsequent references to the removed level yieldundefined. Removing document levels is the most common use of the unloadMovie() function:

      unloadMovie("_level1");
      _level1.unloadMovie();

    Using unloadMovie() with instances

    When applied to an instance (whether the instance is manually or programmatically created), unloadMovie() removes the contents of the clip, but it does not remove the clip itself! The timeline and canvas of the clip are removed, but an empty shell remains on stage. That shell can be referenced until the instance is removed permanently via removeMovieClip() or until the span of frames on which the instance resides ends. Furthermore, any onClipEvent() handlers on the shell remain active.

    This “partial” deletion of the instance presents an interesting possibility; it lets us maintain a generic container clip whose contents can be changed repeatedly via loadMovie() and unloadMovie(). For example, we could use a single clip to load sections of a web site or images in a photo album. The following series of statements demonstrates the technique with an instance calledclipA (though in a real application, these statements would include the appropriate preloader code):

    clipA.loadMovie("section1.swf");  // Load a document into clipA
    clipA.unloadMovie();              // Unload the document, leaving clipA intact clipA.loadMovie("section2.swf");  // Load another document into
    clipA

    One note of caution with this approach: when used on an instance, unloadMovie() removes all custom properties of the clip contained by the instance. Physical properties—such as_xand_alpha—persist, but custom variables, functions, and event handler callbacks are lost.

    If you use the global function form of unloadMovie() with a nonexistent clip or level instance as its argument, the clip from which you invoked the unloadMovie( ) function will, itself, unload.

    For example, if_level1is undefined, and we issue the following code from the main timeline of_level0, then_level0will unload:

      unloadMovie(_level1);

    Yes, there’s some logic to this behavior, but we’ll cover that later in this chapter under “Method Versus Global Function Overlap Issues.” You can avoid the problem by using a string when specifying theclipOrLevel argument of unloadMovie() or by checking explicitly thatclipOrLevel  exists before unloading it. Here’s an example of each approach:

      unloadMovie("_level1");    // clipOrLevel specified as a string
      if (_level1) {             // Explicit check to make sure level exists
       
    unloadMovie(_level1);
      }

    Using removeMovieClip() to Delete Instances

    To delete instances created via duplicateMovieClip(), attachMovie() or createEmptyMovieClip(), we can use removeMovieClip(). We delete an instance when it is no longer needed in our application, such as when an enemy spaceship is destroyed or an alert window is closed. Note that removeMovieClip() works on duplicated or attached instances only. It cannot delete a manually created instance or a main movie. Like unloadMovie(), removeMovieClip() can be used in both method and global function form (though the syntax is different, the effect is the same):

      removeMovieClip(theClip)       // Global function
      theClip.removeMovieClip()      // Method

    In the global function form of removeMovieClip(),theClip is a string indicating the path to the clip to remove. Because movie clips are converted to paths when used as strings,theClip  can also be a movie clip reference. In the method form of removeMovieClip(),theClip must be a reference to a movie clip object.

    Unlike using unloadMovie(), deleting an instance via removeMovieClip() completely obliterates the entire clip object, leaving no shell or trace of the clip and its properties. When we execute theClip.removeMovieClip( ), future references totheClip  yieldundefined.

    Removing Manually Created Instances Manually

    Clip instances created manually in the Flash authoring tool have a limited life span—they are removed when the playhead enters a keyframe that does not include them. Hence, manually created movie clips live in fear of the almighty blank keyframe.

    Remember that when a movie clip disappears from the timeline, it ceases to exist as a data object. All variables, functions, methods, and properties that have been defined inside it are lost. Therefore, if we want a clip’s information or functions to persist, we should be careful about removing the clip manually, and we should ensure that the span of frames on which the clip resides extends to the point where we need that clip’s information. (In fact, to avoid this worry entirely, we should attach most permanent code to a frame in the main movie timeline or to the_globalobject.) To hide a clip while it’s present on the timeline, simply set the clip’s_visibleproperty tofalse. Setting a clip’s_xproperty to a very large positive number or very small negative number will also hide it from the user’s view, but this approach is discouraged because the clip is still rendered off screen, consuming resources.

    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

    - Using XML and ActionScript with Flex Applica...
    - Interfaces and Events with ActionScript and ...
    - Manipulating Data with ActionScript in Flex ...
    - ActionScript Syntax for Flex Applications
    - ActionScript in Flex Applications
    - A Closer Look at Apollo`s File System API
    - Using the File System API
    - ActionScript 101
    - Flash Buttons
    - Advanced Flash Animation
    - Creating Your First Animated Movie with Flas...
    - Flash: Building Blocks
    - Building Preloaders
    - Fun Things to Do with Movie Clips in Flash MX
    - Referencing Movie Clips in Flash MX







    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway