PDA

View Full Version : tricky AS problem - passing type:Function as a parameter


antonielloj
11-05-2004, 06:02 PM
hey all. i have a prototype function called changeIt that essentially alters any mc property at any rate (ill spare you that majority of the code because it works fine). but... i wanted to add a feature that allows the user to call a SECOND function when the first call is finished. in other words, if one wants to change an mc's _xscale from 50 to 200, then, once the mc is at _xscale=200, bring the _alpha down to 0, etc etc. hence the intended purpose of the last two parameters of the function (newObject and whenComplete). the setup of the function looks like this:

MovieClip.prototype.changeIt = function(prop:String, newVal:Number, rate:Number, newObject:Object, whenComplete:Function){
// ... lots of code that works

// ... when the function is completed
if (newObject) newObject[whenComplete];

// ... some more clean-up code that works
}

so, a call to this function might look like this:
mc1.changeIt("_y",150,2,_root.mc2,changeIt("_alpha",300,2));

The problem is that when the function is called, it is called TWICE immediately, and both calls are always invoked on mc1. im assuming this has to do with the fact that im passing a parameter of type:Function. however, is there any other way to call the function a second time when it has parameters that need to be passed into it?

im going crazy. please help! :)

cheers
j

splict
11-05-2004, 06:42 PM
Not sure off hand but I think you would want to do it along the lines of:

mc1.changeIt("_y", 150, 2, _root.mc2, changeIt, "_alpha" ,300, 2);

Notice that the function is named without parenthesis. You would then call it inside your code passing all of the following arguments by getting them from arguments.

annexion
11-05-2004, 06:56 PM
if (newObject){
newObject[whenComplete]();
}


That should work for a method call, although, I don't know what whenComplete is.

antonielloj
11-05-2004, 07:22 PM
annexion - that syntax is fine when the method being called doesnt take any parameters, but what about when it does, and those parameters are being fed in by the parent function?

splict - i see where youre going with your idea, but i would like to make my function versatile enough to accept any number of methods, each one containing a different number of parameters. so, my question is, if im grabbing the remaining arguments of the function using arguments.length, then what is the syntax to pass those parameters into the new function via newObject[whenComplete]() ?

Xeef
11-05-2004, 07:58 PM
hi check this :

function a() {
X = [];
if (arguments.length>1) {
for (q=0; q<arguments.length; q++) {
X[q] = arguments[q];
}
} else {
if (arguments<>arguments[0].split(",")) {
X = String(arguments[0]).split(",");
} else {
X = arguments[0];
}
}
trace("OUT PUT :"+X.shift());
if (X.length>0) {
return arguments.callee(X);
}
}
a("a", "b", "c");
trace("----------------");
a("Hallo", "this", "is", "a", "test");

splict
11-05-2004, 09:49 PM
antonielloj,
good point. At first thought, you could extract the leftover parameters and convert the function call to a string and use eval. I'm not a fan of eval so there might be a better way - I'll think it over. Since you are using prototypes, I'm guessing you are programming in AS1.0 (even though you are in the 2.0 forum)? Just wondering because you could (in AS2.0) throw the eval statement inside a try catch block - might help things a little bit.