Skip to main content
Inspiring
Aug 10, 2006 9:24 PM UTC
Answered

Is there a better way to do this?

I've got a menu I'm programing, but I am starting to forsee it being a lot of code.. Is there a way to cut down this code specifically? I've got it so that the alpha level of a button goes down to 60 onRollOver and the alpha goes back to 100 onRollOut. Is there a better way to do this? Here is my code:

calendar.onRollOver = function() {
calendar._alpha = 60;
}
calendar.onRollOut = function() {
calendar._alpha = 100;
}
programs.onRollOver = function() {
programs._alpha = 60;
}
programs.onRollOut = function() {
programs._alpha = 100;
}
media.onRollOver = function() {
media._alpha = 60;
}
media.onRollOut = function() {
media._alpha = 100;
}
bios.onRollOver = function() {
bios._alpha = 60;
}
bios.onRollOut = function() {
bios._alpha = 100;
}
serving.onRollOver = function() {
serving._alpha = 60;
}
serving.onRollOut = function() {
serving._alpha = 100;
}
This topic is closed to new replies. Start a new post to keep the conversation going.
RothrockCorrect answer
Inspiring
Aug 10, 2006 10:33 PM UTC
The answer is yes. However you might want to give up your nice easy names and do something like this:

for(var i=0;i<5;i++){
this["myButton"+i].onRollover=function(){
this._alpha=60;
}
this["myButton"+i].onRollOut=function(){
this._alpha=100;
}
}

In that case your buttons would need to be named myButton0, myButton1, and so on. You could even make this a bit snazzier by creating an array to hold the names of your buttons, the label that will show, different colors, etc. whatever you wanted. Then step through the array and apply the values. There are lots of ways to do this, experiment and play around.

5 replies

RothrockCorrect answer
Inspiring
Aug 10, 2006 10:33 PM UTC
The answer is yes. However you might want to give up your nice easy names and do something like this:

for(var i=0;i<5;i++){
this["myButton"+i].onRollover=function(){
this._alpha=60;
}
this["myButton"+i].onRollOut=function(){
this._alpha=100;
}
}

In that case your buttons would need to be named myButton0, myButton1, and so on. You could even make this a bit snazzier by creating an array to hold the names of your buttons, the label that will show, different colors, etc. whatever you wanted. Then step through the array and apply the values. There are lots of ways to do this, experiment and play around.
Translate
Inspiring
Aug 11, 2006 12:31 AM UTC
Thanks for the reply. I'm just going to go ahead and assume that it works great. I won't be able to try it until work tommorow.

Thanks again,

Josh
Translate
Inspiring
Aug 10, 2006 10:21 PM UTC

"Josh Pratt" <webforumsuser@macromedia.com> wrote in message news:ebg860$npj$1@forums.macromedia.com...
> I've got a menu I'm programing, but I am starting to forsee it being a lot of
> code.. Is there a way to cut down this code specifically? I've got it so that
> the alpha level of a button goes down to 60 onRollOver and the alpha goes back
> to 100 onRollOut. Is there a better way to do this?

If you want to assign button actions in a loop you can use numbered buttons like btn1, btn2 etc. or you can use an array of button
names in the loop.
Unless you are planning to make a lot more things happen with the buttons it doesn't seem worth all the trouble. If you just want
to make some common rollover and rollout actions you can make a function that all buttons can use by sending their instance names to
the function like this..

// buttons
//-----------------------------------
calendar.onRollOver = function() {
rollOverStuff(this);
}
//-----------------------------------
calendar.onRollOut = function() {
rollOutStuff(this);
}
//-----------------------------------

// common functions
//-----------------------------------
function rollOverStuff(btn)
{
btn._alpha = 20;
btn._y = 55; // button drops down a little
}

//-----------------------------------
function rollOutStuff(btn)
{
btn._alpha = 100;
btn._y = 50; // button back up
}

//-----------------------------------

tralfaz


Translate