# Shorter Code

**URL:** <https://forum.kirupa.com/t/shorter-code/2800>\
**Category:** flash\
**Created:** [April 5, 2002, 10:41am UTC](https://forum.kirupa.com/t/shorter-code/2800 "2002-04-05T10:41:18Z")\
**Posts on this page:** 4\
**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:** [April 5, 2002, 10:41am UTC](https://forum.kirupa.com/t/shorter-code/2800/1 "2002-04-05T10:41:18Z")

</div>

Is there a shorter version of this code?\r\rfunction change (colorNumber, colour) {\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 0) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color0.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 1) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color1.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 2) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color2.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 3) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color3.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 4) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color4.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r&nbsp &nbsp &nbsp &nbsp if (colorNumber == 5) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp color5.setRGB(colour);\r&nbsp &nbsp &nbsp &nbsp }\r}\r\r\rI tried this:\r\rfunction change(name, colour) {\r name.setRGB(colour);\r}\r\rIf I call this function from some other code, syntax is this\r\rchange(“color” add colorNumber, 0x999999);\r\r…but it didn’t work.\r\rCould somebody please help me?

---

<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:** [April 5, 2002, 10:56am UTC](https://forum.kirupa.com/t/shorter-code/2800/2 "2002-04-05T10:56:35Z")

</div>

The function was almost perfect. In a dot syntax call, you can’t use a variable in the string of objects, without having a distinguishing marker. The A/S engineers picked [].\r\rso\r\rfunction change(name, colour) {\r[name].setRGB(colour);\r}\r\rshould work… if I’m reading this correctly.

---

<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:** [April 5, 2002, 11:29am UTC](https://forum.kirupa.com/t/shorter-code/2800/3 "2002-04-05T11:29:29Z")

</div>

Why not something like

```auto
 for (i=0;i<=5;i++) {\r\rthis["color"+i].setRGB(colour);\r\r}

```

It might work.\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:** [April 8, 2002, 4:40pm UTC](https://forum.kirupa.com/t/shorter-code/2800/4 "2002-04-08T16:40:22Z")

</div>

I solved it.\r\rI did this instead.\r\rfunction changeColor (colorNumber, colour) {\r (eval(“color” add colorNumber)).setRGB(colour);\r}\r\rI tried your suggestions. It still didn’t work.\r\rThanks for helping.
