myArray = ["homeText.txt","galleryText.txt", "linksText.txt", "aboutmeText.txt", "emailmeText.txt"];
for (i=0; i<5; i++) {
var mc = button.duplicateMovieClip("button"+i, i+1);
mc.id = i;
mc._y += _root["button"+(i-1)]._y-10;
mc.onRelease = function() {
trace(myArray[this.id]);
};
}
We store the loop iterator as a property of the duplicated movieclip (to which a reference is stored in the variable mc to avoid the repeated associative array referencing), because when the button is being clicked, the loop has already finished and the loop iterator is lost. The loop sets the onRelease handler, but the actual click on the movieclip happens after the for loop has finished (unless you can click a movieclip faster than your processor can execute for loops ;)). So to fix this, we store the iterator as a property during the for loop so that it’s saved (mc.id), and we can then use it again in the onRelease handler