Skip to main content
April 2, 2007
Question

Issue with var inside function

  • April 2, 2007
  • 2 replies
  • 171 views
I have a for loop that is adding button functionality to 16 buttons on the stage. It is targeting the button fine with no problem using the var i as the button number. The issue is inside the button function I have another nested button function for the on state of the button which I also use the var i to specify the url number I want associated with the particular button but it is always outputting 17. I don't even understand how it is getting that number. Here is the code.

This topic has been closed for replies.

2 replies

April 2, 2007
thanks. your code works great
April 2, 2007
i is defined in the for loop, and by the time any of the onPress events are triggered the for loop has completed and i is equal to 17. I'm assuming you want to store the var i onto the MovieClip as a unique id of sorts, so that's what you should do:

this["b"+i].id = i;
this.onPress = function() {
_root.gotoURL(this.id);
trace (this.id);