Creating Interaction with ActionScript > Creating complex interactivity > Setting color values

 

Setting color values

You can use the methods of the built-in Color object to adjust the color of a movie clip. The setRGB method assigns hexadecimal RGB (red, green, blue) values to the object. The following example uses setRGB to change an object's color based on user input.

 
To set the color value of a movie clip:

1

Select a movie clip on the Stage.

2

In the Property inspector, enter carColor as the instance name.

3

Create a button named color chip, place four instances of the button on the Stage, and name them red, green, blue, and black.

4

Select frame 1 in the main Timeline and choose Window > Actions.

5

To create a new Color object, in the Actions toolbox, click the Objects category, then click Movie and Color, double-click new Color, and choose _root.carColor for the target. Enter myColor = in the Expression text box.

Your code should look like this:

myColor = new Color(_root.carColor);

6

To associate an event with an object, in the Actions toolbox, click the Objects category, then click Movie, Movie Clip, and Events, and double-click onRelease. Enter the button instance name—either _root.red, _root.green, _root.blue, or _root.black—in the Object text box.

7

In the Actions toolbox, click the Objects category; then click Movie, Color, Methods, and double-click setRGB. Enter the Color object name myColor in the Object text box. Enter the hexadecimal representation for the color in the Parameter text box:

Color

Hexadecimal value

Red

0xff0000

Green

0x00ff00

Blue

0x0000ff

Black

0x000000


8

Repeat steps 6 and 7 for all four colors, so that your code looks like this:

myColor = new Color(_root.carColor)
_root.blue.onRelease = function(){
	myColor.setRGB(0x0000ff)
}
_root.red.onRelease = function(){
	myColor.setRGB(0xff0000)
}
_root.green.onRelease = function(){
	myColor.setRGB(0x00ff00)
}
_root.black.onRelease = function(){
	myColor.setRGB(0x000000)
}

9

Choose Control > Test Movie to change the color of the movie clip.

For more information about the methods of the Color object, see Color (object) in the ActionScript Dictionary.