# Having probs with the for loop and an array

**URL:** <https://forum.kirupa.com/t/having-probs-with-the-for-loop-and-an-array/185437>\
**Category:** Uncategorized\
**Created:** [April 13, 2006, 11:32am UTC](https://forum.kirupa.com/t/having-probs-with-the-for-loop-and-an-array/185437 "2006-04-13T11:32:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SelectionTool](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@SelectionTool](https://forum.kirupa.com/u/SelectionTool)\
**Post date:** [April 13, 2006, 11:32am UTC](https://forum.kirupa.com/t/having-probs-with-the-for-loop-and-an-array/185437/1 "2006-04-13T11:32:26Z")

</div>

made some code for dynamically adding buttons that contain links to other sites.

here is the code:

for (var i:Number = 0; i\<6; i++) {  
var h:Array = [“[http://www.cnn.com", “[URL=“http://visualdesigner.net/”]http://visualdesigner.net/”, “[URL=“http://www.reebok.com”]http://www.reebok.com”, “[URL=“http://www.macromedia.com”]http://www.macromedia.com”, “[URL=“http://www.adobe.com”]http://www.adobe.com”, "[URL=“http://www.nike.com”]http://www.nike.com](http://www.cnn.com)”];  
var k:String = “b\_mc”+i;

b\_mc.duplicateMovieClip(k, i, b\_mc);  
this[k].\_x = 0;  
this[k].\_y = (b\_mc.\_height+2)_i;  
this[k].onRelease = function() {  
[COLOR=red]this.getURL(h_)  
[/COLOR] };  
trace(h\*);  
}

on the part I highlighted in red is the part giving me problems.Everytime I test the movie the output window keeps telling to check my urls and see if they are properly addressed.Where did I go wrong???:puzzle:

---

<div class="post-metadata">

**Author:** ![Mirandir](https://avatars.discourse-cdn.com/v4/letter/m/a5b964/32.png) [@Mirandir](https://forum.kirupa.com/u/Mirandir)\
**Post date:** [April 13, 2006, 12:52pm UTC](https://forum.kirupa.com/t/having-probs-with-the-for-loop-and-an-array/185437/2 "2006-04-13T12:52:58Z")

</div>

The problem is that h\* will only be valid as long as the for-loop runs. When the onRelease event fire only “i” can have just about any value, if it still exists!

What you need to do is either stor the contents of h\* in a variable inside the button and later use that. Or you need to store the current value of i and use that to later refer to the right element of the array.

Also you should put the assignment of the array to before the for-loop as it is now it’ll only steal reasurces.

Examples:

```auto

var h:Array = ["http://www.cnn.com", "http://visualdesigner.net/", "http://www.reebok.com", "http://www.macromedia.com", "http://www.adobe.com", "http://www.nike.com"];
for (var i:Number = 0; i<6; i++) {
var k:String = "b_mc"+i;

b_mc.duplicateMovieClip(k, i, b_mc);
this[k]._x = 0;
this[k]._y = (b_mc._height+2)*i;
this[k].url = h*; 
this[k].onRelease = function() {
    this.getURL(this.url);
};
trace(h*);
}

```

OR:

```auto

var h:Array = ["http://www.cnn.com", "http://visualdesigner.net/", "http://www.reebok.com", "http://www.macromedia.com", "http://www.adobe.com", "http://www.nike.com"];
for (var i:Number = 0; i<6; i++) {
var k:String = "b_mc"+i;

b_mc.duplicateMovieClip(k, i, b_mc);
this[k]._x = 0;
this[k]._y = (b_mc._height+2)*i;
this[k].num = i; 
this[k].onRelease = function() {
    this.getURL(h[this.num]);
};
trace(h*);
}

```

/Mirandir

---

<div class="post-metadata">

**Author:** ![SelectionTool](https://avatars.discourse-cdn.com/v4/letter/s/ac8455/32.png) [@SelectionTool](https://forum.kirupa.com/u/SelectionTool)\
**Post date:** [April 13, 2006, 1:03pm UTC](https://forum.kirupa.com/t/having-probs-with-the-for-loop-and-an-array/185437/3 "2006-04-13T13:03:38Z")

</div>

Thanks bro.That goes to show you learn something every day.Working now perfectly:afro: right on
