3.1. Setting the Color of a Movie Clip

Problem

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

Solution

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

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

Tip

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 ...

Get Actionscript Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.