Archive for September, 2009

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.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

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

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.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

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.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

Two handlers are in this script. The first one is an onClipEvent (load) handler that sends the movie clip to the last frame, in this case frame 60:

onClipEvent (load) {
gotoAndStop(60);
}

This handler is called only once, when the movie clip first appears.

The second handler is an onClipEvent (enterFrame) handler that executes once per frame. It pushes the movie clip to the previous frame:

onClipEvent (enterFrame) {
prevFrame();
}

Test the movie to see it in action. In the example movie, 06reverse.fla, you can see the same “gear animation” animation play backward.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

For now, let’s write a simple movie clip script that demonstrates how it works. We’ll make a movie clip run backward.

We will start the movie clip at the last frame and then use a prevFrame command to send the clip back one frame at a time.

Start a new movie. Place a simple, animated movie clip on the screen. You can use the “gear animation” from the previous example.

In this case, it doesn’t matter what you name the movie clip. We will never need to refer to it by name because the script will be attached directly to the movie clip.

Select the movie clip and bring up the Actions panel. The title of the Actions panel should be Actions—Movie Clip.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

The sample movie 06enterframe.fla contains a simple movie clip with this script attached to it. Run it, and your Output window will look like this:

This clip has been loaded.
This clip has entered a new frame.
This clip has entered a new frame.
This clip has entered a new frame.
This clip has entered a new frame.

This last line will repeat as long as you let the movie run. This timed repetition will allow us to do all sorts of things in the hours to come. It will be the bedrock of our scripts.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

You have already seen how to attach movie clips to frames and buttons. Now it is time to attach them to movie clips.

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:

onClipEvent (load) {
trace(”This clip has been loaded.”);
}

The load event happens when a movie clip first appears on the screen. It happens one time only.

Remember that when you stop the main timeline, the movie clips on the main timeline continue to animate. That fact is important to movie clip scripts because the next event, enterFrame, occurs every time a new frame is reached in the movie clip. Even when the main timeline is stopped, the movie clip continues to move along its own timeline, looping back to the beginning when it reaches its end. This means that enterFrame events happen at a continuous rate. Whenever an enterFrame event occurs, our onClipEvent (enterFrame) handler will be called. Here is a sample:

onClipEvent (enterFrame) {
trace(”This clip has entered a new frame.”);
}

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

Another way to do this would be to use the keyword this. When you use this, you are referring to the current level. So this at the root level is the same thing as _root. However, this inside the movie clip “gears” will be the same as gears. the following three lines mean the same thing at the root level:

gears.gotoAndStop(7);
_root["gears"].gotoAndStop(7);
this["gears"].gotoAndStop(7);

So which one should you use? The advantage of using _root and this is that you can refer to movie clips by variables. For instance, you could do this:

var whichClipToUse = “gears”;
this[whichClipToUse].stop();

The advantage of using this over _root is that you will not always have everything happening at the root level. Sometimes movie clips will issue commands to other movie clips at lower levels, and you will need to use this to make it work. Therefore, this wins out as the best way to refer to movie clips. However, in simple cases, it will be better to just refer to movie clips by name like we did in the last example.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

You can also use _parent to target the level exactly one above the current level. So, if you are one movie clip down from the root level, and you use _parent, it is the same as using _root. However, if you are two levels down, _parent means the level above, whereas _root means two levels above.

It can help to number the levels. The root level, which is the main timeline, is level 0. A movie clip on the root level is at level 1. If there is a movie clip inside that movie clip, it is at level 2. From level 2, _parent refers to level 1, and _root refers to level 0.

So what about the other way? If you are at level 0, and you want to refer to a movie clip named “gears”, you have already seen that you can refer to it by name. You can also use the term _root followed by square brackets, with the name of the movie clip inside it. Here are two lines of code that mean exactly the same thing, provided they are at the root level:

gears.gotoAndStop(7);
_root["gears"].gotoAndStop(7);

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

We have already seen the simplest way to target a movie clip. Just use its name, followed by a dot, followed by the command you want to send.

However, there are plenty of other ways to target a movie clip as well. First, let’s learn how to target different levels of the Flash movie.

The most basic target level in a Flash movie is the main timeline. You can target it with the _root keyword. That is an underscore followed by the word “root.”

For instance, if you want to send a gotoAndStop command to the main timeline, you can do this:

_root.gotoAndStop(7);

If you issue this command from the main timeline, there is no need for the _root target; however, it will work either way. But if you are writing code that is inside a movie clip, and you want that movie clip to tell the main timeline one level above it to do something, _root is one way of doing it.

Taken From: Sams Teach Yourself Flash™ MX ActionScript in 24 Hours

Comments No Comments »

  • Partner links