home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  

Book HomeActionScript: The Definitive GuideSearch this book

Chapter 13. Movie Clips

Every Flash document contains a Stage -- on which we place shapes, text, and other visual elements -- and a main timeline, through which we define changes to the Stage's contents over time. The Stage (i.e., the main movie) may contain independent submovies, christened movie clips (or clips for short). Each movie clip has its own independent timeline and canvas (the Stage is the canvas of the main movie) and can even contain other movie clips. A clip that contains another clip is referred to as that clip's host clip or parent clip.

A single Flash document can contain a hierarchy of interrelated movie clips. For example, the main movie may contain a mountainous landscape. A separate movie clip containing an animated character can be moved across the landscape to give the illusion that the character is walking. Another movie clip inside the character clip can be used to independently animate the character's blinking eyes. When the independent elements in the cartoon character are played back together, they appear as a single piece of content. Furthermore, each component can react intelligently to the others -- we can tell the eyes to blink when the character stops moving or tell the legs to walk when the character starts moving.

ActionScript offers detailed control over movie clips; we can play a clip, stop it, move its playhead within its timeline, programmatically set its properties (like its size, rotation, transparency level, and position on the Stage) and manipulate it as a true programming object. As a formal component of the ActionScript language, movie clips may be thought of as the raw material used to produce programmatically generated content in Flash. For example, a movie clip may serve as a ball or a paddle in a pong game, as an order form in a catalog web site, or simply as a container for background sounds in an animation. At the end of this chapter we'll use movie clips as the hands on a clock and the answers in a multiple-choice quiz.

13.1. The "Objectness" of Movie Clips

As of Flash 5, movie clips can be manipulated like the objects we learned about in Chapter 12, "Objects and Classes". We may retrieve and set the properties of a clip, and we may invoke built-in or custom methods on a clip. Unlike other objects, an operation performed on a clip may have a visible or audible result in the Player.

Movie clips are not truly a type of object; there is no MovieClip class or constructor, nor can we use an object literal to instantiate a movie clip in our code. So what, then, are movie clips if not objects? They are members of their very own object-like datatype, called movieclip (we can prove it by executing typeof on a movie clip, which returns the string "movieclip"). The main difference between movie clips and true objects is how they are allocated (created) and deallocated (disposed of, or freed). For details, see Chapter 15, "Advanced Topics". Despite this technicality, however, we nearly always treat movie clips exactly like objects.

So how does the "objectness" of movie clips affect our use of them in ActionScript? Most notably, it dictates the way we control clips and examine their properties. Movie clips can be controlled directly through built-in methods. For example:

eyes.play( );

We can retrieve and set a movie clip's properties using the dot operator, just as we would access the properties of any object:

ball._xscale = 90;
var radius = ball._width / 2;

A variable in a movie clip is simply a property of that clip, and we can use the dot operator to set and retrieve variable values:

myClip.myVariable = 14;
x = myClip.myVariable;

Submovie clips can be treated as object properties of their parent movie clips. We therefore use the dot operator to access "nested" clips:

clipA.clipB.clipC.play( );

and we use the reserved _ parent property to refer to the clip containing the current clip:

_ parent.clipC.play( );

Treating clips as objects affords us all the luxuries of convenient syntax and flexible playback control. But our use of clips as objects also lets us manage clips as data; we can store a movie clip in an array element or a variable and even pass a clip reference to a function as an argument! Here, for example, is a function that moves a clip to a particular location on the screen:

function moveClip (clip, x, y) {
  clip._x = x;
  clip._y = y;
moveClip(ball, 14, 399);

Throughout the rest of this chapter, we'll learn the specifics of referencing, controlling, and manipulating movie clips as data objects.

Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.