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”];
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:
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:
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:
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
Thanks bro.That goes to show you learn something every day.Working now perfectly:afro: right on