Groups | Blog | Home
all groups > flash actionscript > may 2006 >

flash actionscript : Variable with nested function


kanag
5/18/2006 10:16:30 PM
I create a button in a loop, this means there is a button at each iteration. I
pass variables to create it and then I add a function to respond when clicked.

Here is what the code looks like:
button = _root.attachMovie(...);
button._x = xofs;
button._y = 200;
...

button.onRelease = function() {
trace("button clicked, surl: "+surl);
};

The problem is that when I test the movie, they always give the same url no
mather wich one I click. The variable surl is also changing at each iteration.


blemmo
5/18/2006 10:43:26 PM
The line
trace("button clicked, surl: "+surl);
uses the value of 'surl' at the time it is executed, so it takes the last
value assigned to 'surl'. You would have to store the proper value with the MC:
button.link = surl;
...
button.onRelease = function() {
trace("button clicked, surl: "+this.link);
};
or pass the 'surl' variable by value, through using a function:
function defineRelease(mc, url){
mc.onRelease = function(){
trace("url = "+url);
}
}
// in the loop:
defineRelease(button,surl);

hth,
blemmo
AddThis Social Bookmark Button