Over on DIRECT-L the other day, a question (thread: "FLASH: how to pass color string from lingo ") arose about how to pass a color value to a Flash sprite from Lingo. A couple of responses discussed how to pass the value in as a string or as a number, both of which need to be converted to colors within Flash.
In many cases, whatever you do in Flash, you’ll need to work with ActionScript’s Color object to do things like change the color transform of a movie clip. I’ll tell you now how to do that directly from Lingo.
The basic ActionScript procedure for changing the color of a movie clip is this:
- Create a Color object associated with a movie clip.
- Create a variable containing a duplicate of the transform of the Color object.
- Modify the transform variable’s properties.
- Set the transform of the movie clip’s Color object.
Assume you’ve got a Flash sprite in channel 1 with a movie clip named test at the root level. To create and associate an ActionScript Color object (co) with the movie clip:
co = sprite (1).newObject ("Color", sprite (1).test)
To make a copy (tr) of the Color object’s transform:
tr = co.getTransform ()
The transform of the ActionScript Color object is an object containing eight properties:
- ra
- red multiplier (0 – 100); mulltiplied by the current red value
- rb
- red offset; added to the multiplied red value (final value has a minimum of 0 and a maximum of 255)
- ga
- green multiplier (0 – 100); mulltiplied by the current green value
- gb
- green offset; added to the multiplied green value (final value has a minimum of 0 and a maximum of 255)
- ba
- blue multiplier (0 – 100); mulltiplied by the current blue value
- bb
- blue offset; added to the multiplied blue value (final value has a minimum of 0 and a maximum of 255)
- aa
- alpha multiplier (0 – 100); mulltiplied by the current alpha value
- ab
- alpha offset; added to the multiplied alpha value (final value has a minimum of 0 and a maximum of 255)
These properties can be set directly once access to the transform object has been established. For an unmodified movie clip, the color transform will be: {ra: 100, rb: 0, ga: 100, gb: 0, ba: 100, bb: 0, aa: 100, ab: 0}
, which is basic black.
Changing the movie clip to red requires two lines: one to modify the copy of the color transform and one to apply the transform to the movie clip’s Color object.
tr.rb = 255
co.setTransform (tr)
The result on the red value of the color transform is that its original value of 0 is multiplied by 100, then 255 is added to it, making the result 255.Once the transform has been reapplied to the Color object, the movie clip changes color. Changing the same clip to blue requires you to suppress the red as well as setting the blue (and of course setting the transform):
tr.rb = 0
tr.bb = 255
co.setTransform (tr)
And finally, you cna also change the alpha:
tr.aa = 0
tr.ab = 127
co.setTransform (tr)
An alternative method that skips the whole transform issue is the setRGB method of the Color object. You perform just step 1 above (create a Color object associated with a movie clip), then pass the method a numeric value that represents red * 256 * 256 + green * 256 + blue. Then you’re done. Here’s an example that sets the movie clip to red:
co.setRGB (16711680)
As John Doe notes in the final post (as of 11 May) on the thread, the Color class has been deprecated in Flash 8 (something I hadn’t noticed), and replaced by the ColorTransform object in the flash.geom package. I haven’t had to figure out how to access that from Lingo yet, though, and since you can only use it in ActionScript if the package has been explicitly included when the SWF was exported, it may pose some problems down the road.