# \[MX\] Array and For Loop Code

**URL:** <https://forum.kirupa.com/t/mx-array-and-for-loop-code/58116>\
**Category:** flash\
**Created:** [July 19, 2004, 10:28pm UTC](https://forum.kirupa.com/t/mx-array-and-for-loop-code/58116 "2004-07-19T22:28:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![foochuck](https://avatars.discourse-cdn.com/v4/letter/f/dfb087/32.png) [@foochuck](https://forum.kirupa.com/u/foochuck)\
**Post date:** [July 19, 2004, 10:28pm UTC](https://forum.kirupa.com/t/mx-array-and-for-loop-code/58116/1 "2004-07-19T22:28:39Z")

</div>

Hello,

Is there any reason why this code wouldn’t work:

```auto

stop();

var mediaAry = new Array("audio","downloads","photogallery","video");

for (i=1; i<=mediaAry.length; i++) {
	this["btn" + i].index = i;
	this["btn" + i].onRelease = function() {
		gotoAndStop("blank");
		btnMCTL.gotoAndStop("blank");
		imagesTL.gotoAndStop(mediaAry[this.index - 1]);
	}
};

```

FYI - btnMCTL is a timeline (\_global.btnMCTL = this;) and imagesTL is another timeline (\_global.imagesTL = this;) and have both been tested and are working correctly.

I also have 4 buttons named - btn1, btn2, btn3, and btn4 on the timeline where this frame script exists.

The line of code in question is this:

```auto

imagesTL.gotoAndStop(mediaAry[this.index - 1]);

```

When I replace it with a number like this:

```auto

imagesTL.gotoAndStop(mediaAry[0]);

```

It works, otherwise, it won’t work.

Thanks

:rd:

---

<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:** [July 20, 2004, 9:14am UTC](https://forum.kirupa.com/t/mx-array-and-for-loop-code/58116/2 "2004-07-20T09:14:05Z")

</div>

```auto

stop();

var mediaAry = new Array("audio","downloads","photogallery","video");

for (i=1; i<=mediaAry.length; i++) {
	this["btn" + i].index = i;
	this["btn" + i].onRelease = function() {
		gotoAndStop("blank");
		btnMCTL.gotoAndStop("blank");
		imagesTL.gotoAndStop(mediaAry[**this["btn"+i].index** - 1]);
	}
};

```

That should do it.
