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

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

Targeting Movie Clips

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.

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.

TIP

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);

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.

  • + Share This
  • 🔖 Save To Your Account