Recipe 3.1 Setting the Color of a Movie Clip

3.1.1 Problem

You want to control the color of a movie clip dynamically (at runtime, rather than during authoring).

3.1.2 Solution

Create a Color object that targets the desired movie clip, and then use the Color.setRGB( ) method.

3.1.3 Discussion

You can't change the color of a movie clip directly. Instead, you must first create a Color object that targets the movie clip of interest, as follows:

my_color = new Color(myMovieClip);

You can alternatively specify the movie clip name as a string:

my_color = new Color("myMovieClip");

The target movie clip, as specified when creating a Color object, must correspond to the name of a movie clip instance on the Stage, not the name of the Library symbol from which the clip is derived. Therefore, you must set the target clip's instance name using the Property inspector before you can target it using a Color object.

A Color object can target a movie clip on any timeline using an absolute or relative reference, such as:

my_color = new Color(_root.myMovieClip); my_color = new Color(_parent.myMovieClip);

Once you have created a Color object, you can use it to control the color of the targeted movie clip instance.

The Color.setRGB( ) method applies a single color value to the movie clip targeted by the Color object (as specified when the Color object was created), filling the entire movie clip shape with a single color. The color value can be of any valid ActionScript numeric format.

The following examples both apply a solid blue color to the targeted movie clip:

my_color.setRGB(0x0000FF);     // Hexadecimal my_color.setRGB(255);          // Decimal

The following examples both apply a solid lime green color to the targeted movie clip:

my_color.setRGB(0xA9FC9C);     // Hexadecimal my_color.setRGB(11140252 );    // Decimal

3.1.4 See Also

Recipe 5.2



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net