Targeting Flash Movie Clips

written by: Milan Midovich; article published: year 2007, month 05;



In: Categories » Computers and technology » Flash » Targeting Flash Movie Clips

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

legal disclaimer

1) Our website is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringements, please read the Terms of service and contact us to investigate the problem.
2) The E-articles directory team is not responsible for inaccuracies, falsehoods, or any other types of misinformation this tutorial may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here. Please read the Terms of service

Useful tools and features

Translate this article to...    Send this article to you or to a friend

Link to this article from your page   
If you like this article (tutorial), please link to it from your web page using the information above. Linking to this page, this is the only way to help us improve our service, the same time providing your visitors with a way to improve their online experience.

related articles

1. How to draw a line using ActionScript
Use the lineStyle( ) method to specify the thickness, color, and transparency of the line. Then use the lineTo( ) method to draw a line from the current pen position to a destination point. Before you can draw anything using ActionScript, you need to tell Flash what kind of line style to use. The line style consists of the line's thickness, color, and alpha value (opacity), and you can set these values using the lineStyle( ) method on the movie clip in which you wish to draw. The line's thickness should be specified in pixels, but an...

2. Drawing an Ellipse using ActionScript
Create a custom MovieClip.drawEllipse( ) method using the Drawing API and invoke it on a movie clip. You can create a method of the MovieClip class to draw an ellipse that is very similar to the drawCircle( ) method. In fact, the drawCircle( ) method is merely a degenerate version of drawEllipse( ), in which the radii in the x and y directions are the same. The custom drawEllipse( ) method accepts four parameters: xRadius The radius of the ellipse in the x direction (major axis). yRadius ...

3. ActionScript: Filling a Shape with a Gradient
Use the beginGradientFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. In a gradient fill, there is a graded change in colors. Flash supports linear gradients, in which one color fades into the next from left to right. Flash also supports radial gradients, in which the colors radiate out from a center point. You can initiate a gradient-filled shape using beginGradientFill( ) in the same way you initiate a solid-filled shape with beginFill( ). The difference is that the call to beginGradientFill( ) require...

4. Scripting Masks in Flash
Use the Drawing API to create a shape and then use MovicClip.setMask( ) to apply the mask. Masks can be used to create unique shapes or visual effects. For example, you can use masks to create wipes and transitions or interesting animations in which only the masked portion of the artwork is visible at a given time. You can even create masks that change shape over time, and use them to mask bitmapped graphics (in movie clips). You can use any movie clip as a mask of another movie clip using the setMask( ) method. The setMask( ...

5. Drawing a Triangle using ActionScript
Create a custom MovieClip.drawTriangle( ) method using the Drawing API and invoke it on a movie clip. You can determine and plot the vertices of a triangle given the lengths of two sides and the angle between them. This is a better approach than specifying the lengths of the three sides because knowing the lengths of two sides and the angle between them always determines a triangle, whereas three arbitrary sides may not fit together to make a triangle. The custom drawTriangle( ) method accepts six parameters: ab ...

6. ActionScript: Filling a Shape with a Solid or Translucent Color
Use the beginFill( ) and endFill( ) methods to initiate and close a shape drawn at runtime. To draw a filled shape, call beginFill( ) prior to any other drawing methods, including the custom methods you have defined such as drawCircle( ) and drawPolygon( ). Invoke endFill( ) after calling other drawing methods to create the shape. You cannot apply a fill to an existing shape drawn at authoring time or runtime. You must invoke beginFill( ) before drawing the shape to be filled. This example creates a solid blue ...

7. Drawing a rectangle using ActionScript
Create a custom MovieClip.drawSimpleRectangle( ) method using the Drawing API and invoke it on a movie clip. To draw a simple rectangle, specify the stroke's attributes using the lineStyle( ) method and then draw four lines using the lineTo( ) method: // Create rectangle_mc with a depth of 1 on the main timeline. _root.createEmptyMovieClip("rectangle_mc", 1); // Specify a one-pixel, solid, black line. rectangle_mc.lineStyle(1, 0x000000, 100); // Draw four lines to form the perimeter ...

8. How to draw a rectangle with rounded corners
You want to draw a rectangle with rounded corners, an offset, or rotation.Create a custom MovieClip.drawRectangle( ) method using the Drawing API and invoke it on a movie clip. The drawSimpleRectangle( ) method is, as the name suggests, quite simple. Let's create a more complex version that also: Draws a rectangle with a specified angle of rotation Let's you specify the rectangle center's coordinates Can draw a rectangle with rounded corners The drawRectangle( ) method accepts...