Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Lost i value within loop 1

Status
Not open for further replies.

frozenpeas

Technical User
Sep 13, 2001
893
CA
Hi,

Everything below works as expected, until the onRelease action is called. The instance names are correct, but the i value within the onRelease comes out as 4 (the length of my array) for every button.

Any ideas? Thank you.

Code:
for(i=0;i<picArray.length;i++){
	//create button on stage
	attachMovie("btn","btn"+i,i);
	this["btn"+i]._x = originX;
	this["btn"+i]._y = originY;
	this["btn"+i].txtNum = i+1;
	originX += offsetX;
	this["btn"+i].onRelease = function(){
		trace(i);
		trace(this._name);
	}
}

frozenpeas
--
Micfo.com Affiliate Program
 
By the time you click on a button the value of the variable "i" has become 4 (otherwise you won't see the 4 buttons on stage!) so that you will get "4" in the output when you click any of those 4 buttons.

If you want to get the number "0" when you click the "btn0", you can extract the number from the name of the button:
[tt]//
...
this["btn"+i].onRelease = function() {
trace(parseInt(this._name.substr(3, 1)));
};
//[/tt]

Or since you're conveniently storing "i+1" in the buttons, you can do:
[tt]//
...
this["btn"+i].onRelease = function() {
trace(this.txtNum-1);
};
//[/tt]

Kenneth Kawamoto
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top