# Simple on.Release problem

**URL:** https://forum.kirupa.com/t/simple-on-release-problem/117015
**Category:** Uncategorized
**Created:** [July 23, 2004, 12:02pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015 "2004-07-23T12:02:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![bobdoe](https://avatars.discourse-cdn.com/v4/letter/b/51bf81/32.png) [@bobdoe](https://forum.kirupa.com/u/bobdoe)
#### Post date: [July 23, 2004, 12:02pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015/1 "2004-07-23T12:02:43Z")

</div>

myArray = [“homeText.txt”,“galleryText.txt”,  
“linksText.txt”, “aboutmeText.txt”, “emailmeText.txt”];  
for(i=0;i\<5;i++){  
button.duplicateMovieClip(“button”+i, i+1);  
\_root[“button”+i].\_y += \_root[“button”+(i-1)].\_y - 10;  
\_root[“button”+i].onRelease = function(){  
trace(myArray\*);  
}  
}

I want button0 to trace myArray[0], and the same for the rest of the buttons.

I wrote seperate code for each button but i found that very tedious. Can someone help me fix this code?

---

<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 23, 2004, 12:15pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015/2 "2004-07-23T12:15:31Z")

</div>

```auto

  myArray = ["homeText.txt","galleryText.txt", "linksText.txt", "aboutmeText.txt", "emailmeText.txt"];
  for (i=0; i<5; i++) {
      var mc = button.duplicateMovieClip("button"+i, i+1);
      mc.id = i;
      mc._y += _root["button"+(i-1)]._y-10;
      mc.onRelease = function() {
          trace(myArray[this.id]);
      };
  }
  

```

We store the loop iterator as a property of the duplicated movieclip (to which a reference is stored in the variable mc to avoid the repeated associative array referencing), because when the button is being clicked, the loop has already finished and the loop iterator is lost. The loop sets the onRelease handler, but the actual click on the movieclip happens after the for loop has finished (unless you can click a movieclip faster than your processor can execute for loops ;)). So to fix this, we store the iterator as a property during the for loop so that it’s saved (mc.id), and we can then use it again in the onRelease handler 🙂

---

<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 23, 2004, 12:34pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015/3 "2004-07-23T12:34:20Z")

</div>

Hey voetsjoeba, thanks a lot for the code. It works beautifully. I was thinking of doing it your way but couldnt implement the code.

On a side not, why do you type in the err…4th person(whichever the “we” perspective is).

---

<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 23, 2004, 12:49pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015/4 "2004-07-23T12:49:03Z")

</div>

You couldn’t implement it ? I just showed you how to implement it ! 😛

I don’t know why I talk in the We form … I always do that wehn explaining code, lol.

---

<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 23, 2004, 12:51pm UTC](https://forum.kirupa.com/t/simple-on-release-problem/117015/5 "2004-07-23T12:51:59Z")

</div>

hhaaha no no…i meant before you showed me how to do it. My mind was on the same track but couldnt code it out.
