Skip to main content
Known Participant
June 14, 2010
Answered

Make an object change colors randomly

  • June 14, 2010
  • 2 replies
  • 982 views

Good afternoon,

I have a simple task that I would like to accomplish but every online tutorial or code suggestion doesn't work; consequently I figured the Adobe forum was best!

I have a simple .fla file with a circle on the stage. I want to set actionscript to make the circle randomly change colors when playing. I've tried all sorts of script commands using math random but to no avail.

Does anyone have any suggestions on how to write the script to randomly change the colors of a shape? Thank you so much!

This topic has been closed for replies.
Correct answer

You can use a ColorTransform object to change the color of a display object. Assuming your shape has an instance name of myShape, then something like so should get you started:

var col:ColorTransform = new ColorTransform();

col.color = Math.random() * 16777215;

myShape.transform.colorTransform = col;

2 replies

Correct answer
June 15, 2010

You can use a ColorTransform object to change the color of a display object. Assuming your shape has an instance name of myShape, then something like so should get you started:

var col:ColorTransform = new ColorTransform();

col.color = Math.random() * 16777215;

myShape.transform.colorTransform = col;

Known Participant
June 15, 2010

Thank you so much. This worked perfectly. I appreciate it!

kglad
Community Expert
Community Expert
June 14, 2010

how often or when do you want it to change colors?


Known Participant
June 15, 2010

Good morning,

I just want it to switch colors a few times (maybe 7?) and then loop over and over.

Thank you,

Jen