Creating Interaction with ActionScript > Setting color values
Setting color values
You can use the methods of the predefined Color object to adjust the color of a movie clip. The setRGB
method assigns hexadecimal RGB (red, green, blue) values to the object, and the setTransform
method sets the percentage and offset values for the red, green, blue, and transparency (alpha) components of a color. The following example uses setRGB
to change an object's color based on user input.
To change the color of the t-shirt, enter a hex value (for example, cc66ff, or 0033cc) in the text field in the movie.
To use the Color object, you need to create an instance of the object and apply it to a movie clip.
To set the color value of a movie clip:
1 | Select a movie clip on the Stage, and choose Window > Panels > Instance. |
2 | Enter the instance name colorTarget in the Name box. |
3 | Drag a text field on the Stage. |
4 | Choose Window > Panels > Text Options and assign it the variable name input. |
5 | Drag a button to the Stage and select it. |
6 | Choose Window > Actions. |
7 | Drag the set variable action from the toolbox to the Script window. |
8 | In the Variable box, enter c. |
9 | In the toolbox, select Objects, then Color, and drag new Color to the Value box. |
10 | Select the Expression check box. |
11 | Click the Target Path button and select colorTarget . Click OK. |
The code in the Script window should look like this: | |
on(release) { c = new Color(colorTarget); } |
|
12 | Drag the evaluate action from the toolbox to the Script window. |
13 | Enter c in the Expression box. |
14 | In the Objects category of the Toolbox list, select Color; then drag setRGB to the Expression box. |
15 | Select Functions and drag parseInt to the Expression box. |
The code should look like this: | |
on(release) { c = new Color(colorTarget); c.setRGB(parseInt(string, radix)); } |
|
16 | For the parseInt string argument, enter input. |
The string to be parsed is the value entered into the editable text field. | |
17 | For the parseInt radix argument, enter 16. |
The radix is the base of the number system to be parsed. In this case, 16 is the base of the hexadecimal system that the Color object uses. The code should look like this: | |
on(release) { c = new Color(colorTarget); c.setRGB(parseInt(input, 16)); } |
|
18 | Choose Control > Test Movie to change the color of the movie clip. |
![]() |
For more information about the methods of the Color object, see their entries in the ActionScript Dictionary.