Functions

[size=+2][color=#680aac][font=Comic Sans MS][size=2]How do you make a function where you type, for example, bladibladiblah(whatever, whatever2, whatever3);, and the whatevers be a variable in the function? Here’s my code:

function colorFade(endRa, endRb, endGa, endGb, endBa, endBb, endAa, endAb, speed) {
speed = speed;
myTransform = new Object();
rA += (endRa-rA)/speed;
rB += (endRb-rB)/speed;
gA += (endGa-gA)/speed;
gB += (endGb-gB)/speed;
bA += (endBa-bA)/speed;
bB += (endBb-bB)/speed;
aA += (endAa-aA)/speed;
aB += (endAb-aB)/speed;
myTransform.ra = rA;
myTransform.rb = rB;
myTransform.ga = gA;
myTransform.gb = gB;
myTransform.ba = bA;
myTransform.bb = bB;
myTransform.aa = aA;
myTransform.ab = aB;
colorFunction = new Color(this);
colorFunction.setTransform(myTransform);
}

Then, on my MC, there’s this:
onClipEvent (enterFrame) {
colorFade(-100, 256, -100, 256, -100, 256, 100, 0, 5);
}

Shouldn’t that pass on the variables I used to call the function to the actual function as endRa, endRb, endGa, etc., etc.?[/size][/font][/color][/size]

Yes it should…doesn’t it work ?

I’m used to strict data typing to make sure my variables have the right data type. That usually prevents a lot of problems. And I also always declare my vars inside functions.

[font=Comic Sans MS][color=#680aac]It only has one problem, and that is, that the variables (endRa, endRb, endGa, endGb, endBa, endBb, endAa, endAb, speed) are not being recognized when the function is called. On my MC I have to code to call the function, including what I want the end variables to be, but it doesn’t work.[/color][/font]

Is this the whole code ? If it is, you might have forgotten to assign an initial value to vars like rA, rB, etc, which are all undefined.
And like that the calculation your functions makes returns undefined as a result too:
rA += (endRa-rA)/speed;
Only endRA and speed are defined.

Try adding vars like this:
var rA = 0;
var rB = 0;
Assuming that they should start at zero ofcourse :slight_smile:

Hope that helps…

Ah, I see what you mean. In that case you’re using the mc’s timeline to call the function, but it’s on the root’s. So try either:
_root.colorFade()
or even better
_parent.colorFade()

or place the onEnterFrame on your main timeline, so you don’t have to worry about anything at all:

mcMyMovieClip.onEnterFrame = function():Void {
colorFade(-100, 256, -100, 256, -100, 256, 100, 0, 5);
};

Hope that works! :slight_smile:
Seems to work for me…color greys out right away :slight_smile: