PDA

View Full Version : Simple Color Changing Function


Nick Toye
06-06-2005, 04:24 PM
Hi,

I've just returned to Flash after a while and I have decided to have a go at using more logical actionscript.

What I want to know is can I set the color of a rollOver just by calling a function?

This is what I have currently:


this.onRollOver = function(){
var buttonColor = new Color(_root.aboutBtn.drop.historyBtn.history_image );
buttonColor.setRGB(0xFFFFFF);
}

this.onRollOut = function(){
var buttonColor = new Color(_root.aboutBtn.drop.historyBtn.history_image );
buttonColor.setRGB(0xCCCCCC);
}


But I want to be able to declare the function initially, and then just call it whenever I need to instead of having to write that code again.

I'm sure it is simple, i'm just not entirely sure on the parameters, i think I need to put in the target clip but like I say I'm not sure.

Thanks in advance

zamfir
06-06-2005, 04:40 PM
Try this:

changeColor = function (i) {
if (i=0) {
var buttonColor = new Color(_root.aboutBtn.drop.historyBtn.history_image );
buttonColor.setRGB(0xFFFFFF);
} else {
var buttonColor = new Color(_root.aboutBtn.drop.historyBtn.history_image );
buttonColor.setRGB(0xCCCCCC);
}
};

then on the frame with the rollover, just call the function and pass it either 0 or 1 to tell it which color you're doing. Should work.

Nick Toye
06-06-2005, 04:43 PM
Sorry, I meant a global function to set the color on rollOver and another global function to set the color on rollOut.

zamfir
06-06-2005, 04:49 PM
Well if you're always using the same type of object, you can do a for...in loop in your function and when it finds the object you're trying to change, have it change the color accordingly. Look up the "for...in" in help, you'll see how useful it can be.

Nick Toye
06-06-2005, 04:51 PM
I was looking for help building a prototype color class that can be called whenever needed and then I presume in the parameters I drop in the instance that I wish to have the color changed.

zamfir
06-06-2005, 05:38 PM
So you didn't want a function, you wanted a class? Well, you seem to be pretty hip and apparently know what you need, so I suggest you just code it. :)

Nick Toye
06-06-2005, 07:21 PM
I'm not that hip, If I was to look at real world I would do this:

name of function/class (target clip)

set the color to (color here)

Then when I need to call the function/class

name of function/class (target clip)

...and thats it. Does that make sense

tg
06-06-2005, 11:20 PM
try:

colorOver = function(o){
var buttonColor = new Color(o);
buttonColor.setRGB(0xFFFFFF);
}

colorOut = function(o){
var buttonColor = new Color(o);
buttonColor.setRGB(0xCCCCCC);
}


//usage:
myButton.onRollOver=function(){
colorOver(this);
};
myButton.onRollOut=function(){
colorOut(this);
};

Nick Toye
06-07-2005, 09:59 AM
Cheers for that tg, if I want to declare it though at the start of my flash movie, so that I can access it anywhere in the movie do I have to declare it as a _global or do I need to create it as a prototype?

tg
06-07-2005, 02:59 PM
depends on the layout of your movie... i can use that just the way it is in 99% of my movies, cause they only have 1 frame.

Nick Toye
06-07-2005, 04:25 PM
I just wanted to see if I could do it, and then if I could I would move on to developing more advanced classes.