Home > Articles > Graphics & Web Design > Dreamweaver & Flash

  • Print
  • + Share This
This chapter is from the book

Task: Clips Controlling Other Clips

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.

Take a look at the example movie 06clipcommunication.fla to see what this looks like before trying to build it yourself.

  1. Create a new Flash movie. Make a movie clip that has 15 frames of animation. Name it "cog".

  2. 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.

  3. On the 15th frame of the movie clip, place the following script:

  4. _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.

  5. 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".

  6. 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.

    NOTE

    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.

  7. Now drag the "cog" movie clip to the work area a second time. Name this instance "cog2". Place the following script on it:

  8. 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.

  9. 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.

  • + Share This
  • 🔖 Save To Your Account