# Problems assigning actions to buttons in a loop

**URL:** <https://forum.kirupa.com/t/problems-assigning-actions-to-buttons-in-a-loop/64592>\
**Category:** Uncategorized\
**Created:** [September 17, 2004, 6:13pm UTC](https://forum.kirupa.com/t/problems-assigning-actions-to-buttons-in-a-loop/64592 "2004-09-17T18:13:28Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![OM2](https://avatars.discourse-cdn.com/v4/letter/o/3ab097/32.png) [@OM2](https://forum.kirupa.com/u/OM2)\
**Post date:** [September 17, 2004, 6:13pm UTC](https://forum.kirupa.com/t/problems-assigning-actions-to-buttons-in-a-loop/64592/1 "2004-09-17T18:13:28Z")

</div>

i’m pulling my hair out over this one. ☹

I have is a movie with a button in it called button.  
i’ve duplicated the movie (manually) several times on the stage.  
the movies have been given instance names of movie1, movie2 etc.  
i’ve then used actionscript to assign actions to the buttons.  
i have the following code:

```auto

for (i = 1; i < 6; i++)
{
 this["movie" + i].button.onRelease = function() 
 { 
   abc(i); 
 }; 
}

function abc(num)
{
 trace(num);
}

```

the problem is that everytime one of the buttons is pressed, it prints out 6.  
i’ve spent hours on this problem. ☹  
i couldn’t see that i’d done anything wrong.

but now having simplified the code…  
i think there is a problem with the passing of the arguement in the fucntion call within the button.

i’ve attached the Flash file. (mx2004 format.)  
i’m kinda desperate and need to solve this problem asap. ☹

any help/suggestions would be appreciated.

thanks.

OM

---

<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:** [September 17, 2004, 7:40pm UTC](https://forum.kirupa.com/t/problems-assigning-actions-to-buttons-in-a-loop/64592/2 "2004-09-17T19:40:03Z")

</div>

> [@OM2](#):
>
> i’m pulling my hair out over this one. ☹
> 
> I have is a movie with a button in it called button.  
> i’ve duplicated the movie (manually) several times on the stage.  
> the movies have been given instance names of movie1, movie2 etc.  
> i’ve then used actionscript to assign actions to the buttons.  
> i have the following code:
> 
> ```auto
> 
> for (i = 1; i < 6; i++)
> {
> this["movie" + i].button.onRelease = function() 
> { 
> abc(i); 
> }; 
> }
> 
> function abc(num)
> {
> trace(num);
> }
> 
> ```
> 
> the problem is that everytime one of the buttons is pressed, it prints out 6.  
> i’ve spent hours on this problem. ☹  
> i couldn’t see that i’d done anything wrong.
> 
> but now having simplified the code…  
> i think there is a problem with the passing of the arguement in the fucntion call within the button.
> 
> i’ve attached the Flash file. (mx2004 format.)  
> i’m kinda desperate and need to solve this problem asap. ☹
> 
> any help/suggestions would be appreciated.
> 
> thanks.
> 
> OM

not seen the file so untested but try  
for (i=1; i\<6; i++) {  
this[“movie”+i].ivar = i;  
this[“movie”+i].button.onRelease = function() {  
abc(this.\_parent.ivar);  
};  
}  
function abc(num) {  
trace(num);  
}

---

<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:** [September 17, 2004, 8:16pm UTC](https://forum.kirupa.com/t/problems-assigning-actions-to-buttons-in-a-loop/64592/3 "2004-09-17T20:16:34Z")

</div>

thank you!  
🙂
