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:

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

And the AS3 equivalent:

  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):

  1. Math.random() * 0xFFFFFF

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


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 ^^

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

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


Ely likes men

Leave a comment