AS2 → AS3: Setting A MovieClip’s Color Dynamically

Download Example Files

The Color class has been deprecated since Flash 8 but was still usable in AS2 to set/get a MovieClip's RGB values. That has since changed in AS3 and here is the new way of doing things.

The old, AS2 way of changing a color of a MovieClip was as follows:

Actionscript:
  1. var c:Color = new Color(box_mc);
  2. c.setRGB(Math.random() * 0xFFFFFF);

And the AS3 equivalent:

Actionscript:
  1. var c:ColorTransform = new ColorTransform();
  2. c.color = (Math.random() * 0xFFFFFF);
  3. box_mc.transform.colorTransform = c;

There is only a one line difference but as you can see AS3 uses the ColorTransform class to achieve this same effect.

And while we are at it, in case anyone was wondering, the way to get a random color in Flash is to write this line (as evidenced by the above code):

Actionscript:
  1. Math.random() * 0xFFFFFF

If you found this post useful, please consider leaving a comment, subscribing to the feed, or making a small donation.

10 Comments

Thanks for your example. You mention 'set/get' a color but how do you get the color of a movieclip in as3? Thanks!

@Jeff: unfortunately i dont think there is an easy way to do this. you'd have to draw the clip to a bitmapdata object and then use getPixel() on it :\ kind of sucks, but thats all I can think of.

I'm new to AS, I've been looking for the way of changing color dynamically for MovieClip for long time, here I got it, thanks!

thanks very much, i found this ^^

Hy,
How can I get in AS3 the color of an instance button on the stage?

@OnLine: Unfortunately there is no easy way of doing this. Your best bet is to convert the clip to a bitmap and use getPixel(). The reason for this is that a button/movieclip/sprite/whatever can have many colors in it.

The <> contains the ColorTransform class aswell as
AS3.

The old AS2 contains the ColorTransform class aswell as
AS3. you fu.... morron

THANK YOU! YOU ARE A LIFE SAVER 😀

Ely likes men

Leave a comment

(required)

(required)