Dynamic function calls in AS 2.0

I am not quite sure if this is even possible with Flash, but I have done it successfully in other programming languages.

At this point I have a function that is given 2 parameters (object and func) and am wanting to be able to call the object’s function (func) within the function that I have (as to make things simple and even be able to just loop through an array calling each function as I go (dynmically).

Here is my current function:

function doFunc(object, event){
  object.event();
}

And I am passing it something like so:

doFunc(test_btn, "doSomething");

where test_btn is a simple button movieclip that has the function doSomething inside of it (like so):

function doSomething(){
  trace("Function called successfully");
}

The only issue here is that it doesn’t seem to work out any way I have tried it. If anyone knows what is going on here (or if this is even possible in AS) I would be really thankful.

Thanks in advance, and hopefully this is allowed…

It’s possible. Do you need to pass the function name instead of the function? If you just pass doSomething instead of “doSomething”, then you can just call event() in doFunc.

Is doSomething defined inside (as a property of) test_btn the syntax that you used implied that it was. If so, you can call the function using a string like so:

object[event]()

If the function is defined on another accessible scope, then you need to target it properly. Most probably, it’s just defined on this.

Thanks a ton it works :slight_smile: