Targeting Flash Movie Clips

by Milan Midovich.

Share
|
Homepage | Submit your article | Contact | TOS
More articles on flash  

You are here: Categories » Computers and technology » Flash

The simplest way to target a movie clip is to 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.

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

Leave a comment or ask a question
Total comments: 0

Flash 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
Move Clip Rotation - A property like _x and _y is the movie clip property _rotation. The _rotation property accepts a value in degrees. A circle is divided into 360 degrees. The values used by _rotation range f (more...)
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...)
How to build an animated button using Flash - Time: 15 Minutes. Difficulty Level: Intermediate Requirements: Flash 8 Assumed Knowledge: Basic action (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...)

 
free content
    Copyright © 2006 - 2012 e-articles.info.
The texts, articles and tutorials in the directory are property of their respective owners and authors.