Recipe 3.7 Transforming a Movie Clip s Current Color

Recipe 3.7 Transforming a Movie Clip's Current Color

3.7.1 Problem

You want to modify a movie clip's color relative to the current color transformation, instead of relative to the author-time color values.

3.7.2 Solution

Use the getTransform( ) and setTransform( ) methods of the Color object that targets the movie clip.

3.7.3 Discussion

The Color.getTransform( ) method returns the transform object last applied to the targeted movie clip. For example, if you had previously applied a transform object that set the movie clip's alpha percentage to 42, getTransform( ) would return an object with the following values:

{ra: 100, rb: 0, ga: 100, gb: 0, ba: 100, bb: 0, aa: 42, ab: 0}

The transform object reflects changes made to a movie clip's color either at authoring time or at runtime. If you modify the color values using the Property inspector at authoring time, those values are indicated in the movie clip's transform object. You can make changes to a movie clip's color at runtime by using the setTransform( ) or setRGB( ) methods of its Color object, and you can adjust the movie clip's _alpha property separately. All runtime changes not just the changes made using setTransform( ) are reflected in the transform object. If you have not applied any color changes at runtime or authoring time then getTransform( ) returns the following value (a neutral transform object):

{ra: 100, rb: 0, ga: 100, gb: 0, ba: 100, bb: 0, aa: 100, ab: 0}

You can modify the properties of the transform object returned by getTransform( ) and then apply the modifications using setTransform( ):

// Create the Color object. my_color = new Color(myMovieClip); // Get the transform object. myTransformObject = my_color.getTransform(  ); // Set the green percentage of all colors within the movie clip to 50% of the current // value. myTransformObject.ga = 50; // Apply the transform object. my_color.setTransform(myTransformObject);

The preceding example retains the previously applied transform values, with the exception of ga, which is set to 50. You can instead increment or decrement the properties relative to their current values:

// Get the transform object. myTransformObject = my_color.getTransform(  ); // Increment the red, green, and blue offsets by 10 to brighten the object's colors. myTransformObject.rb += 10; myTransformObject.gb += 10; myTransformObject.bb += 10; // Set the transform object. my_color.setTransform(myTransformObject);

Transformations applied with setTransform( ) occur relative to the colors in the original movie clip symbol, independent of any previous transformations. In other words, the transformations are not cumulative. We simulated a cumulative transformation by basing the new transformation on the previous values, as returned by getTransform( ).



ActionScript Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2005
Pages: 425

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net