# Random color generator question

**URL:** <https://forum.kirupa.com/t/random-color-generator-question/1611>\
**Category:** Uncategorized\
**Created:** [June 4, 2002, 5:19am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611 "2002-06-04T05:19:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 5:19am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/1 "2002-06-04T05:19:47Z")

</div>

Hi Kirupa and everyone else, I just discovered your site, lemme say - GREAT site, love the advanced actionscript tutorials. You bet I will be trying them out this week. \r\rI just tried the random color generator tutorial and it worked, but I want to understand the code instead of just cutting and pasting =). \r\rWhile I think understand what needs to be done in general (ie, changing the RGB values at random), I"m a little confused on the actual lines of code. For example, what does this line do?:\r\r\_root[“r”+myNum+“Diff”] = \_root[“r”+myNum+“New”]-\_root[“r”+myNum+“Old”];\r \_root[“r”+myNum+“Change”] = \_root[“r”+myNum+“Diff”]/10;\r\rCan someone give me a quick explanation of the rest of the code as well? Thanks so much ahead of time. \r\rFuzz

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 5:52am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/2 "2002-06-04T05:52:49Z")

</div>

Can you post the rest of the code too?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 6:05am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/3 "2002-06-04T06:05:59Z")

</div>

Sure, here is the link:\r\r [http://www.kirupa.com/developer/flash5/randomcolor.asp\r\rOr](http://www.kirupa.com/developer/flash5/randomcolor.asp%5Cr%5CrOr), here is the code:\r\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\ronClipEvent (load) {\r myNum = \_name;\r}\ronClipEvent (enterFrame) {\r \_root[“r”+myNum+“Diff”] = \_root[“r”+myNum+“New”]-\_root[“r”+myNum+“Old”];\r \_root[“r”+myNum+“Change”] = \_root[“r”+myNum+“Diff”]/10;\r \_root[“g”+myNum+“Diff”] = \_root[“g”+myNum+“New”]-\_root[“g”+myNum+“Old”];\r \_root[“g”+myNum+“Change”] = \_root[“g”+myNum+“Diff”]/10;\r \_root[“b”+myNum+“Diff”] = \_root[“b”+myNum+“New”]-\_root[“b”+myNum+“Old”];\r \_root[“b”+myNum+“Change”] = \_root[“b”+myNum+“Diff”]/10; \r swatchColor = new Color(this);\r swatchColor.setRGB(\_root[“r”+myNum+“Change”]\<\< 16 | \_root[“g”+myNum+“Change”] \<\< 8 |\r\r \_root[“b”+myNum+“Change”]);\r \_root[“r”+myNum+“Old”] -= \_root[“r”+myNum+“New”]-\_root[“r”+myNum+“Change”];\r \_root[“g”+myNum+“Old”] -= \_root[“g”+myNum+“New”]-\_root[“g”+myNum+“Change”];\r \_root[“b”+myNum+“Old”] -= \_root[“b”+myNum+“New”]-\_root[“b”+myNum+“Change”];\r}\r\r\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\*\rThis part goes on the button that changes the colors:\r\ron (release) {\r for (i=1; i\<5; i++) {\r \_root[“r”+i+“New”] = random(255);\r \_root[“g”+i+“New”] = random(255);\r \_root[“b”+i+“New”] = random(255);\r \_root[“b”+i+“New”] = random(255);\r }\r}\r\r\rThanks!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 10:05am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/4 "2002-06-04T10:05:06Z")

</div>

That tutorial was from a long time ago, and it was based upon a FLA that I found. I merely used the useful portions of the code and made it into a tutorial ☹ So I won’t be able to help you there. I am fairly certain that there are far more efficient ways of doing this animation!\r\rCheers,\rKirupa

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 10:28am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/5 "2002-06-04T10:28:36Z")

</div>

Hi Fuzz. That’s a hard piece of actionscript you’re dealing with.

```auto
 onClipEvent (enterFrame) {\r\r _root["r"+myNum+"Diff"] = _root["r"+myNum+"New"]-_root["r"+myNum+"Old"];\r\r _root["r"+myNum+"Change"] = _root["r"+myNum+"Diff"]/10;

```

Basically, this movie checks all the time the new values of a variable called r1New, r2New… that are set with the button with this line :

```auto
 _root["r"+i+"New"] = random(255);

```

This is a very useful syntax which evaluates what’s between the square brackets.\r\rThere’s also a variable r1Old, r2Old… which is the old color of the object. Then r1Diff, r2Diff is the amount by which you have to shift the colors, and since you want it to be smooth, you shift it by 1/10 at a time.\r\rDoes that make more sense to you ?\r\rpom 0]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 11:04am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/6 "2002-06-04T11:04:10Z")

</div>

Many thanks y’all.\r\rwow, that is complicated. Thanks for trying to explain it a bit to me. I’m still a little confused but I think I think I’ll figure it out. I didn’t realize it was an old tutorial, perhaps Flash MX has more capabilities to make it easier now.\r\rCya\rFuzz.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [June 4, 2002, 11:21am UTC](https://forum.kirupa.com/t/random-color-generator-question/1611/7 "2002-06-04T11:21:21Z")

</div>

No it doesn’t for the simple reason that it isn’t difficult, it’s just the writing that is very disconcerting.\rI can try and explain something better, if there’s something in particular.\r\rpom 0]
