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

actionscript studs: pass function reference?? 1

Status
Not open for further replies.

theotrain

Programmer
Mar 5, 2003
150
MX
I know that in flash you can pass objects or movie clip references as arguments to a function. but you cant (as far as i can tell) send a function reference as an argument to a function.
[tt][blue]
myFunction(10, myOtherFunction() );
//this is obviously no good because it would send the final product of myOtherFunction(),not a reference to it.
[/blue][/tt]
It also seems like you cant cheat it by sending the function name as a string, then doing an eval on it to invoke the function,
[tt][blue]
myFunction(10, "myOtherFunction(5)" );

function myFunction(x,y) {
eval(y);
}
//be great if this worked, but no good
[/blue][/tt]
because eval(expression) only works when the (expression) is a variable name. eval("5+5") wont give you 10. similarly if you pass a string "process(10)" as an argument , it seems there is no way of getting back from that string to running code. It seems like a huge mistake not have a function that will take the value of any string and simply run it as code. do you know of any such thing?

i wanted to do something pretty simple, just make a generic timer function that i could pass a function reference and a couple variables and it would run the function every (x) frames for (y) iterations.

there is problably a simpler way of accomplishing this that im not familiar with or thinking of, but any advice on the [blue]"function timer" function[/blue] or the [blue]passing of function reference[/blue] or the [blue]switching from a string to running code[/blue] issue much appreciated.

thanks> otrain
 
You can do it like this by passing the function as a variable:

Code:
myFunction = function (passed) {
	passed();
};
//
var func = function () {
	trace('function!');
};
//
myFunction(func);
 
wow. so simple. I havent tried the code yet but that is SWEET.

i dont know why but I have never defined a variable as a function, and it would never have occured to me to try!

thanks a heap Wangbar!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top