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

duplicate code problem

Status
Not open for further replies.

MJB3K

Programmer
Jul 16, 2004
524
0
0
GB
Hi, i have this code for about 10buttons and i was wondering if there is a way to do this without having to write it out like 10times:
Code:
//irc
this.btnIRC.onRollOver = function() {
	this.gotoAndPlay(2);
};
this.btnIRC.onRollOut = function() {
	this.gotoAndPlay(11);
};
//home
this.home.onRollOver = function() {
	this.gotoAndPlay(2);
};
this.home.onRollOut = function() {
	this.gotoAndPlay(11);
};

Is there any way of doing this, or is this the only way?

Regards,

Martin

Computing Help And Info:
 
Now I'm no Flash expert by any means, but here's an idea.

Isn't it possible to put that code into a global function and then just have each button call it? I know Flash has functions, I just have yet to use them so I'm not exaclty sure of their scope.

Hope it helps
 
this will affect all buutons that do not have assigned actions

Button.prototype.onRollOver = function() {
this.gotoAndPlay(2);
};

similar for onRollOut just place code on frame 1 of main movie

 
Doesn't seem to work on my movieclip rollovers. They aren't proper component buttons, just movieclips.

Regards,

Martin

Computing Help And Info:
 
Hi Martin,

Heres a thought (currently at work so can't test this to see if it works).

1. Set up the instance names of the movieclips as btn1,btn2, btn3 etc

2. Use a for loop to assign the rollover and rollout events to a function,
Code:
for(cntr=1;cntr<=10;cntr++){
this["btn"+cntr].onRollOver = rolloverfunc;
this["btn"+cntr].onRollOut = rolloutfunc;
}
3. Set up the rolloverfunc to gotoAndPlay(2)

4. Set up the rolloutfunc to gotoAndPlay(11)

As an alternative you can also setup an array of the instance names and loop through them if you dont want to name them as btn1, btn2 etc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top