How can I set the variable in currItem.onRelease = function() according to the for loop? Cause, what it does is actually on click on any of the buttons, it will return me the last value of the loop. Meaning if on click on any of the buttons say if there are 8 buttons, and button1 has var = 1, button2 var =2 and so on, I clicked on any of the buttons, it returns var = 8 for all. How can I make it such that on click button1 return 1, button5 return 5 and so on?
for (var i = 0; i<numBtns; i++)
{
currItem = currMenu.attachMovie(“menuBtn”,“btn” + i + “_mc”, i + 1);
currMenuTitle = menuTitle.attachMovie(“menuNameMC”,“title”,0);
currMenuTitle._x = 100;
currMenuTitle._y = 100;
currMenuTitle.menuName.text = recData.MuscleDesc0;
That’s because the assignment is only made after the for loop has been completed, onRelease. A for loop executes about as fast as your CPU can execute it (read: hella fast), there’s no way any visitor could click a button that fast. The for loop is only setting the handler, it’s not actually executed yet. That only happens when the user clicks the button (or when you would explicitly call the onRelease function, but that’s very rarely done). When the button is actually clicked and the assignment is made, the for loop is already done, and thus the i is the last value of the for loop at that time.
To solve this, assign the i to the movieclip at loop runtime. That way, the i is stored in the movieclip as the for loop continues, and it can use it when needed later, in the onRelease event handler.
for (var i = 0; i<numBtns; i++) {
currItem = currMenu.attachMovie("menuBtn","btn" + i + "_mc", i + 1);
currMenuTitle = menuTitle.attachMovie("menuNameMC","title",0);
currMenuTitle._x = 100;
currMenuTitle._y = 100;
currMenuTitle.menuName.text = recData.MuscleDesc0;
currItem._x = x;
currItem._y = y + i*currItem._height;
currItem.trackAsMenu = true;
btnName = eval("recData.Name"+i);
currItem.btnName.text = btnName;
// store the i as id during runtime for later use
currItem.id = i;
currItem.onRelease = function(){
// user clicks, far later than the for loop has executed, and the stored value can be used
myVar = this.id;
}
}
Thanks! it works. But how come when I do onRelease() function, I set the var as this.somevars and after that I loadMovie(“some.swf” ,1) into the current swf and what is clickable behind the some.swf meaning the level 0 swf is still clickable on the swf on level1.