Learning ActionScript 2.0 in Adobe Flash |
|
|
|
| Working with Movie Clips > About controlling movie clips with ActionScript | |||
You can use global ActionScript functions or the methods of the MovieClip class to perform tasks on movie clips. Some methods of the MovieClip class perform the same tasks as functions of the same name; other MovieClip methods, such as hitTest() and swapDepths(), don't have corresponding function names.
The following example shows the difference between using a method and using a function. Each statement duplicates the instance my_mc, names the new clip new_mc, and places it at a depth of 5.
my_mc.duplicateMovieClip("new_mc", 5);
duplicateMovieClip(my_mc, "new_mc", 5);
When a function and a method offer similar behaviors, you can select to control movie clips by using either one. The choice depends on your preference and your familiarity with writing scripts in ActionScript. Whether you use a function or a method, the target timeline must be loaded in Flash Player when the function or method is called.
To use a method, activate it by using the target path of the instance name, a dot (.), and then the method name and parameters, as shown in the following statements:
myMovieClip.play(); parentClip.childClip.gotoAndPlay(3);
In the first statement, play() moves the playhead in the myMovieClip instance. In the second statement, gotoAndPlay() sends the playhead in childClip (which is a child of the instance parentClip) to Frame 3 and continues to move the playhead.
Global functions that control a timeline have a target parameter that let you specify the target path to the instance that you want to control. For example, in the following script startDrag() targets the instance the code is placed on and makes it draggable:
my_mc.onPress = function() {
startDrag(this);
};
my_mc.onRelease = function() {
stopDrag();
};
The following functions target movie clips: loadMovie(), unloadMovie(), loadVariables(), setProperty(), startDrag(), duplicateMovieClip(), and removeMovieClip(). To use these functions, you must enter a target path for the function's target parameter to indicate the target of the function.
The following MovieClip methods can control movie clips or loaded levels and do not have equivalent functions: MovieClip.attachMovie(), MovieClip.createEmptyMovieClip(), MovieClip.createTextField(), MovieClip.getBounds(), MovieClip.getBytesLoaded(), MovieClip.getBytesTotal(), MovieClip.getDepth(), MovieClip.getInstanceAtDepth(), MovieClip.getNextHighestDepth(), MovieClip.globalToLocal(), MovieClip.localToGlobal(), MovieClip.hitTest(), MovieClip.setMask(), MovieClip.swapDepths().
For more information about these functions and methods, see their entries in the ActionScript 2.0 Language Reference.
For a sample source file, animation.fla, that illustrates scripted animation in Flash, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Animation folder to access the sample.
For samples of photo gallery applications, see the Flash Samples page at www.adobe.com/go/learn_fl_samples. Download and decompress the Samples zip file and navigate to the ActionScript2.0/Galleries folder to access these samples:
These files provide examples of how to use ActionScript to control movie clips dynamically while loading image files into a SWF file, which includes scripted animation.
|
|
|
|