As of Flash 5, movie clips can be manipulated like the objects we learned about in Chapter 12. We can retrieve and set the properties of a clip, and we can invoke built-in or custom methods on a clip. An operation performed on a clip often has a visible or audible result in the Flash Player.
Movie clips are not truly a type of object, but they are object-like; although we can neither create movie clips via a class constructor nor use an object literal to instantiate a movie clip, we can instantiate movie clips using attachMovie( ) , duplicateMovieClip( ) and createEmptyMovieClip( ) (introduced in Flash Player 6). 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 other objects is how they are allocated (created) and deallocated (disposed of, or freed). For details, see:
Despite this technicality, however, we nearly always treat movie clips exactly as we treat objects.
So how does the "objectness" of movie clips affect our use of them in ActionScript? Most notably, it allows us to call methods on clips and examine their properties, just as we can for other objects. Movie clips can be controlled directly through built-in methods. For example:
eyes_mc.play( );
We can retrieve and set a movie clip's properties using the dot operator, just as we access the properties of any object:
ball_mc._xscale = 90; var radius = ball_mc._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:
theClip_mc.someVariable = 14; x = theClip_mc.someVariable;
Nested movie 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 reference in an array element or a variable, and we can 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 moveClipTo (clip, x, y) { clip._x = x; clip._y = y; } moveClipTo(ball_mc, 14, 399);
In this chapter, we'll cover the specifics of referencing, controlling, and manipulating movie clips as data objects.
All individual movie clips are instances of the MovieClip class, which defines the properties, methods, and event handlers supported by movie clips. The MovieClip class can be manipulated like other built-in classes; we can overwrite its methods or add new methods to it using the techniques we studied in Chapter 12. For example, the following code adds a new method, getArea( ), to all movie clips:
MovieClip.prototype.getArea = function ( ) { return this._width * this._height; };
As we'll see in Chapter 14, as of Flash MX, the MovieClip class can even be used as the superclass for new classes (i.e., new subclasses can be derived from the MovieClip class). For full coverage of every property, method, and event handler supported by the MovieClip class, see the Language Reference.