# Scoping variables aarghh

**URL:** https://forum.kirupa.com/t/scoping-variables-aarghh/4951
**Category:** Uncategorized
**Created:** [August 26, 2002, 1:36pm UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951 "2002-08-26T13:36:42Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Stanley](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@Stanley](https://forum.kirupa.com/u/Stanley)
#### Post date: [August 26, 2002, 1:36pm UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/1 "2002-08-26T13:36:42Z")

</div>

for (var i = 2; i\<=maxvalue; i++)  
{duplicateMovieClip(“clip”, “clip”+i, i);  
…  
…  
and so on

so you see I want to duplicate a movieclip here as much as ‘maxvalue’ times. It works fine. But inside the duplicated movieclips is another movieclip/button and its (unique) behaviour is controlled by the following script: (I also placed this in the “for”-loop (?))  
…  
clipnumber = eval(“clip”+i);  
clipnumber.submovie.onRelease = function() {  
getURL(“detailpage”+i,“target”);  
};  
…

but this i variable is not reachable from within the function. I suppose I am making very fundamental mistakes, but can somebody help me out and show me the right way to do this?

thanks guys  
Stanley

---

<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: [August 26, 2002, 3:56pm UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/2 "2002-08-26T15:56:07Z")

</div>

Maybe try adding your button code after the duplicate movie clip code

Just change it to look like \_root.mainClip.yourButton.onRelease

Maybe that will help.

---

<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: [August 27, 2002, 9:19am UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/3 "2002-08-27T09:19:54Z")

</div>

I had the same problem a couple of days ago 🙂  
You have to define the i variable for each movie (especially because it’s in a loop)

```auto
for (var i = 2; i<=maxvalue; i++) 
{
      mc=duplicateMovieClip("clip", "clip"+i, i);
      // you don't need clipnumber, you manipulate mc 
      mc.i=i;
      mc.submovie.onRelease = function()
      {
            getURL("detailpage"+_parent.i,"target");
      }
};

```

Something like that.

pom :asian:

---

<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: [August 28, 2002, 8:00am UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/4 "2002-08-28T08:00:29Z")

</div>

Mhhh…  
This looks like a good solution. I’ll give it a try…  
thanks man 🙂

---

<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: [August 29, 2002, 8:51am UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/5 "2002-08-29T08:51:08Z")

</div>

I’m not quite there yet. In other words, I don’t get this thing to work…  
Could you explain your code a little more, pom?

Stan

---

<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: [August 29, 2002, 8:57am UTC](https://forum.kirupa.com/t/scoping-variables-aarghh/4951/6 "2002-08-29T08:57:45Z")

</div>

Sure.

```auto
for (var i = 2; i<=maxvalue; i++) 
{
      mc=duplicateMovieClip("clip", "clip"+i, i);
      // in fact, mc=this["clip"+i]
      mc.i=i;
      // we define the i variable of this particular movie clip
      mc.submovie.onRelease = function()
      // submovie should be a clip that already exist in clip
      {
            // I put _parent because you have to go one level up in
            // the hierarchy. If this doesn't work, you could try to put
            // mc.i instead
            getURL("detailpage"+_parent.i,"target");
      }
};

```

Otherwise post your code or your fla.

pom :asian:
