orthopedic pain management

Flashuser

  • Pin It

Tips & Tricks 12: Change the color of a MovieClip using ColorTransform

If you ever wonder how to change the color of a MovieClip in Actionscript 3 here’s a very useful tip. All you have to do is use the ColorTranform class. There are many methods to be used, one that come in handy is the color property which I’ll show it to you.

This property replace the old fashion Actionscript 2 setRGB() method. Here’s the code for changing the color of a MovieClip in AS2:

[as3]
var myColor:Color = new Color(myMovie);
myColor.setRGB(0x4455ff);
[/as3]

where myMovie is an instance of a MovieClip.

For Actionscript 3 the code isn’t so complicated and here it is:
[as3][/as3]
var newColor:ColorTransform = myMovie.transform.colorTransform;
newColor.color = 0xFF5522;
myMovie.transform.colorTransform = newColor;
[as3][/as3]

To see how the code works have a look at the example below:

The code for the example is here:
[as3]
import flash.geom.ColorTransform;

square1.buttonMode = true;
square1.useHandCursor = true;
square1.mouseChildren = false;
square1.addEventListener("mouseOver", rollOverHandler);
square1.addEventListener("mouseOut", rollOutHandler);

square2.buttonMode = true;
square2.useHandCursor = true;
square2.mouseChildren = false;
square2.addEventListener("mouseOver", rollOverHandler);
square2.addEventListener("mouseOut", rollOutHandler);

function rollOverHandler(e:MouseEvent) {
var newColor:ColorTransform = e.target.transform.colorTransform;
if (e.target.name =="square1") {
newColor.color = 0xFF5522;
} else {
newColor.color = 0x0066CC;
}
e.target.transform.colorTransform = newColor;

}

function rollOutHandler(e:MouseEvent) {
var newColor:ColorTransform = e.target.transform.colorTransform;
if (e.target.name =="square1") {
newColor.color = 0x0066CC;
} else {
newColor.color = 0xFF5522;
}
e.target.transform.colorTransform = newColor;
}
[/as3]

Author: Alin

Passionate about web and graphic design, photography and climbing. Follow me at Twitter , Facebook and Google+