|
| You are here: Categories » Computers and technology » Flash
|
Movie clips can also control other movie clips. By using the _root or _parent keyword, you can send your commands up one level. Then, by using the name of the movie clip you want to address, you can send the commands back down to another clip. Here is an example. Suppose that you want the movie clip "gears1" to send a command to its sibling, "gears2":
_parent.gears2.gotoAndStop(7);
If "gears1" and "gears2" are at level 1, _parent addresses level 0. Adding "gears2" addresses the command back down to level 1, but to another movie clip entirely. Another way to do this would be with square brackets:
_parent["gears2"].gotoAndStop(7);
Now let's use that technique to create a movie with three movie clips. The first one has a movie clip script that advances it one frame at a time. Inside this movie clip is a script triggered on the 15th frame. It tells the next movie clip to move forward one frame. This second movie clip does the same thing to a third movie clip. The result is that the first movie clip animates quickly, one frame per normal movie frame. The second movie clip animates one frame for every 15 frames that the first clip animates. The third movie clip animates one frame for every 15 frames the second clip animates.
-
Create a new Flash movie. Make a movie clip that has 15 frames of animation. Name it "cog".
-
Inside the movie clip, place a stop() script on the first frame. This prevents it from animating all by itself. Instead, we will control its animation through ActionScript.
-
On the 15th frame of the movie clip, place the following script:
_parent[clipToTell].nextFrame();
gotoAndStop(1);
This code does two things. First, it tells a sibling movie clip with the name stored in the variable clipToTell that it should advance to the next frame. Second, it sends itself back to the first frame to start again.
-
Now we just have to define the variable clipToTell. We'll do this in the movie clip script, so exit the editing of the "cog" movie clip and return to the main timeline. Place an instance of the "cog" movie clip in the work area and name it "cog1".
Now attach a movie clip script to it. Here is the script:
onClipEvent (load) {
clipToTell = "cog2";
} onClipEvent (enterFrame) {
nextFrame();
}
The first thing that happens when the movie clip starts is that the variable clipToTell is set to "cog2". This means that when the movie clip gets to frame 15, it uses the previous script in step 3 to tell "cog2" to advance one frame.
The onClipEvent (enterFrame) handler is used to advance this movie clip by one frame for each main movie frame.
It can be confusing to see that the movie clip script and the frame scripts inside the movie clip are at the same level. After all, you can only get at and edit the movie clip script while viewing the main timeline, and you can only get at and edit the movie clip's frame scripts by viewing the movie clip's timeline. Despite this, these scripts are all at the movie clip level. This is why the global variable clipToTell is available to both.
-
Now drag the "cog" movie clip to the work area a second time. Name this instance "cog2". Place the following script on it:
onClipEvent (load) {
clipToTell = "cog3";
}
This is all the second movie clip needs. It does not need a onClipEvent (enterFrame) handler because it does not advance one frame for every frame the main movie does. Instead, it gets its instruction to advance from "cog1".
The second clip, however, has a value of "cog3" for the clipToTell variable. That means that when it gets to frame 15, it tells "cog3" to advance by one frame.
-
Create a third instance of the "cog" movie clip. Name this one "cog3". No script is needed on this movie clip at all. There will be no "cog4" in this example, so "cog3" does not need to worry about telling another movie clip that it is time to advance.
This movie demonstrates more than just clip-to-clip communication. It also demonstrates how movie clip scripts and a movie clip's frame scripts can share a global variable. This global is available only inside the movie clip and not to other sibling movie clips or the main timeline. |
|
Leave a comment or ask a question
|
|
Total comments: 0
Disclaimer
- The e-articles directory is not responsible for any and all copyright infringements by writers and authors. If you suspect the information contained by this page for any copyright infringements, please contact us to investigate the issue
|
|
|
Using Mathematical Operators in ActionScript - You want to modify something over time, such as the rotation or position of a movie clip.
Use the compound assignment operators to change a variable or property in increments. Or, if increm (more...)
Enhancing Standalone Projector - You want to create an enhanced Standalone Projector with features such as borderless playback, custom titles, no Flash menus, and so on. Use a third-party tool such as SWF Studio or SWFKit to c (more...)
Creating Flash Buttons - Buttons are one of the three main types of symbols that you use in Flash. The others are movie clips and graphics.
Making a New Button
There are many ways to create a button. One (more...)
Bouncing Ball Script - Start a new Flash movie. Create a movie clip that has a ball graphic inside it.
You can name the instance of the movie clip, myClip, but our code will not depend on the name of the clip. (more...)
ActionScript: Creating Reusable Code - You want to perform a series of actions at various times without duplicating code unnecessarily throughout your movie.
Create a function and then call (i.e., invoke) it by name whenever you (more...)
3D Scaling with ActionScript - Although Flash is not capable of real 3D graphics, the kind seen in popular computer games, you can create the illusion of 3D by using scaling.
Scaling an object is a great way to give your (more...)
The Dot Syntax - Something else that you will be seeing a lot of as you learn ActionScript is dot syntax. Dot syntax is a way of grouping objects and functions that is used in many object-oriented programming (more...)
Movie Clip Scripts - Movie clip scripts, like button scripts, use handlers. Instead of using the on keyword that button scripts use, we will use the onClipEvent keyword. Here is an example of a movie clip script:
(more...)
Decoding an RGB Value - You want to extract the red, green, and blue components from an RGB value returned by Color.getRGB( ).
Use the bitshift right and bitwise AND operators.
You can extract the red, gre (more...)
|
|
|