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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

actionscript dynamic variable name in loop 1

Status
Not open for further replies.

ericaalicen

Programmer
Dec 20, 2001
53
US
I'm having an issue looping through and accessing variable names dynamically in actionscript.

This code to call one directly works.
Code:
	mcSquares.square1.onRollOver = function(): Void {
		mcSquares.square1._visible = false;
	};

When I put the code in the loop and try to make it work for square1-square5, it doesn't work.
Code:
var nSquares:Number = 6;

for(var i:Number = 1; i < nSquares; i++)
{
	trace(mcSquares.square[i]);
	mcSquares.square[i].onRollOver = function(): Void {
		mcSquares.square[i]._visible = false;
	};
}

Can anyone tell me what I'm doing wrong?
 
For anyone interested, this the working code:

Code:
var nSquares:Number = 6;

for(var i:Number = 1; i <= nSquares; i++)
{
	trace(mcSquares["square"+i]);
	mcSquares["square"+i].ID = i;
	mcSquares["square"+i].onRollOver = function(): Void {
		mcSquares["square"+this.ID]._visible = false;
	};
}

Thanks oldnewbie.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top