# \[as2\] array loop help

**URL:** <https://forum.kirupa.com/t/as2-array-loop-help/264536>\
**Category:** flash\
**Created:** [June 26, 2008, 10:37pm UTC](https://forum.kirupa.com/t/as2-array-loop-help/264536 "2008-06-26T22:37:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![craigashby](https://avatars.discourse-cdn.com/v4/letter/c/9dc877/32.png) [@craigashby](https://forum.kirupa.com/u/craigashby)\
**Post date:** [June 26, 2008, 10:37pm UTC](https://forum.kirupa.com/t/as2-array-loop-help/264536/1 "2008-06-26T22:37:47Z")

</div>

i have a number of buttons that use tweens for rollovers. instead of making a rollover function for each button, i made a generic rollover that uses an array.

everything works perfect when building the array manually, even though i forgot the quotes around each entry. adding them however, breaks it.

```auto
var adIcons_array:Array = new Array(adIconCover1, adIconCover2, adIconCover3, adIconCover4, adIconCover5);

```

this is the output  
\_level0.adIconCover1,\_level0.adIconCover2,\_level0.adIconCover3,\_level0.adIconCover4,\_level0.adIconCover5

i decided to make a simple loop to build the array automatically, since it is very predictable. i only have 5 here, but eventually there will be 5 groups of 12.

```auto
var adIcons_array:Array = new Array();

for (i=0; i<6; i++) {
	adIcons_array* = "adIconCover"+i;
}

```

this is the output  
adIconCover0,adIconCover1,adIconCover2,adIconCover3,adIconCover4,adIconCover5

the difference is one extra entry, adIconCover0, and losing \_level0.

i figure an extra entry doesn’t matter much, and i understand the \_level0 is basically the same as \_root, so i tried adding that into the array builder, but still no luck.

```auto
var adIcons_array:Array = new Array();

for (i=0; i<6; i++) {
	adIcons_array* = "_root.adIconCover"+i;
}

```

\_root.adIconCover0,\_root.adIconCover1,\_root.adIconCover2,\_root.adIconCover3,\_root.adIconCover4,\_root.adIconCover5

i finally got it to work with this, which gave me the \_level0 results again.

```auto
var adIcons_array:Array = new Array();

for (i=0; i<6; i++) {
	adIcons_array* = eval("_root.adIconCover"+i);
}

```

i know it’s working so i should probably just be happy and continue working, but i’m hoping someone can spread some light on when to use eval, and when not to and why the quotes broke the initial array.

i found this post from a couple days back that is related:

> **[array from object paths](https://www.kirupa.com/forum/showthread.php?t=302028)**
>
> Got a web design/development question or just want to hang out with the most awesome people on the planet? Drop on in!

thank you

---

<div class="post-metadata">

**Author:** ![nathan99](https://avatars.discourse-cdn.com/v4/letter/n/96bed5/32.png) [@nathan99](https://forum.kirupa.com/u/nathan99)\
**Post date:** [June 27, 2008, 12:26am UTC](https://forum.kirupa.com/t/as2-array-loop-help/264536/2 "2008-06-27T00:26:18Z")

</div>

```auto
var root:MovieClip = this;
var adIcons_array:Array = [];

for (var i:Number = 0; mc=root["adIconCover"+(++i)];) {
	adIcons_array.push(mc);
}
trace(adIcons_array);

```
