Random color generator question

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

Can you post the rest of the code too?

Sure, here is the link:\r\r http://www.kirupa.com/developer/flash5/randomcolor.asp\r\rOr, 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!

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

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

 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 :

 _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]

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.

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]