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!

Using a for loop to streamline code? 1

Status
Not open for further replies.

carmenMiranda

Programmer
May 22, 2003
47
GB
I have some AS2 code that has a lot of duplication to control various buttons and MovieClips and was wondering if it's possible to streamline this code using a 'for' loop, like I would when attaching MovieClips dynamically.

Here's the code as it's currently written:

Code:
//----------- INIT VARS -------------------------

var color01:String = "0xE24C9B";
var color02:String = "0x009BDB";
var color03:String = "0x50B848";
var color04:String = "0xF58220";

var base01:MovieClip = timer01_mc.timerBase_mc;
var base02:MovieClip = timer02_mc.timerBase_mc;
var base03:MovieClip = timer03_mc.timerBase_mc;
var base04:MovieClip = timer04_mc.timerBase_mc;

var btn01:Button = timer01_mc.rollBtn_btn;
var btn02:Button = timer02_mc.rollBtn_btn;
var btn03:Button = timer03_mc.rollBtn_btn;
var btn04:Button = timer04_mc.rollBtn_btn;

var bar01:MovieClip = timer01_mc.timerBar_mc;
var bar02:MovieClip = timer02_mc.timerBar_mc;
var bar03:MovieClip = timer03_mc.timerBar_mc;
var bar04:MovieClip = timer04_mc.timerBar_mc;

var url01:String = "[URL unfurl="true"]http://www.url01.com";[/URL]
var url02:String = "[URL unfurl="true"]http://www.url02.com";[/URL]
var url03:String = "[URL unfurl="true"]http://www.url03.com";[/URL]
var url04:String = "[URL unfurl="true"]http://www.url04.com";[/URL]

//-------------- BUTTON CONTROL -------------------------

btn01.onRollOver = function(){ baseCol01 = new Color(base01); baseCol01.setRGB(color01);
};
btn01.onRollOut = function(){ baseCol01 = new Color(base01); baseCol01.setRGB(0xCCCCCC);
};
btn01.onRelease = function(){ getURL(url01, "_blank");
};

btn02.onRollOver = function(){ baseCol02 = new Color(base02); baseCol02.setRGB(color02);
};
btn02.onRollOut = function(){ baseCol02 = new Color(base02); baseCol02.setRGB(0xCCCCCC);
};
btn02.onRelease = function(){ getURL(url02, "_blank");
};

btn03.onRollOver = function(){ baseCol03 = new Color(base03); baseCol03.setRGB(color03);
};
btn03.onRollOut = function(){ baseCol03 = new Color(base03); baseCol03.setRGB(0xCCCCCC);
};
btn03.onRelease = function(){ getURL(url03, "_blank");
};

btn04.onRollOver = function(){ baseCol04 = new Color(base04); baseCol04.setRGB(color04);
};
btn04.onRollOut = function(){ baseCol04 = new Color(base04); baseCol04.setRGB(0xCCCCCC);
};
btn04.onRelease = function(){ getURL(url04, "_blank");
};

Can anyone show me how to construct the correct type of 'for' loop to streamline this code?
 
Try something like this...

Code:
for(i=1;i<5;i++){
	_level0["btn0"+i].num = i;
	
	_level0["btn0"+i].onRollOver = function(){
		trace("OVER!"+this);
		trace(_level0["base0"+this.num]);
		trace(_level0["color0"+this.num]);
		baseCol01 = new Color(_level0["base0"+this.num]);
		baseCol01.setRGB(_level0["color0"+this.num]);
	};

	_level0["btn0"+i].onRollOut = function(){
		trace("OUT!"+this);
		baseCol01 = new Color(_level0["base0"+this.num]);
		baseCol01.setRGB(0xCCCCCC);
	};

	_level0["btn0"+i].onRelease = function(){
		trace("URL:"+this.num);
		getURL(_level0["url0"+this.num], "_blank");
	};
};

Regards. CLICK HERE TO DONATE.

VISIT MY FORUM
TO GET YOUR OWN FREE WEBSITE HOSTING
 
Thank you very much for taking the time to do that. I can follow the syntax you're using and I think this is what I was struggling with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top