572 Creating Interaction with ActionScript
Setting color values
You can use the methods of the built-in ColorTransform class (flash.geom.ColorTransform) to
adjust the color of a movie clip. The
rgb property of the ColorTransform class assigns
hexadecimal red, green, blue (RGB) values to the movie clip. The following example uses
rgb
to change an object’s color, based on which button the user clicks.
To set the color value of a movie clip:
1. Create a new Flash document and save it as setrgb.fla.
2. Select the Rectangle Tool and draw a large square on the Stage.
3. Convert the shape to a movie clip symbol and give the symbol an instance name of car_mc
in the Property inspector.
4. Create a button symbol named colorChip, place four instances of the button on the Stage,
and name them
red_btn, green_btn, blue_btn, and black_btn.
5. Select Frame 1 in the main Timeline, and select Window > Actions.
6. Add the following code to Frame 1 of the main Timeline:
import flash.geom.ColorTransform;
import flash.geom.Transform;
var colorTrans:ColorTransform = new ColorTransform();
var trans:Transform = new Transform(car_mc);
trans.colorTransform = colorTrans;
7.
To make the blue button change the color of the car_mc movie clip to blue, add the
following code to the Actions panel:
blue_btn.onRelease = function() {
colorTrans.rgb = 0x333399; // blue
trans.colorTransform = colorTrans;
};
The preceding snippet of code changes the rgb property of the color transform object and
reapplies the color tranform effect to the
car_mc movie clip whenever the button
is pressed.