PDA

View Full Version : Interpreting String as function name ActionScript 3.0



mfsiddiq
November 21st, 2007, 04:36 AM
Hi
I want to know if there is any way a string can be interpreted as function name.I need this to call functions dynamically.Below is a code snippet which works fine if the function is in the same class as the function call.But what if i want to call a function present in another class or a different library.Any suggestions will be appreciated.

public var actionRef:String = "getit"

public function initApp():void
{
button.label="Click"
canvas.addChild(button);
button.addEventListener(MouseEvent.CLICK,callMe);
}

public function callMe(event:MouseEvent):void
{
//if (this.hasOwnProperty(actionRef))
this[actionRef]();

}
public function getit():void
{
trace("inside getit")
}
Cheers
mfsiddiq

andreiashu
November 21st, 2007, 04:38 AM
I had the same question a while ago :) Look here: http://www.kirupa.com/forum/showthread.php?t=274165
Cheers
EDIT: oops, sorry, didn't read all your post :p So if you want to call a function from other class shouldn't you first instantiate that class ? Smth like:

var myInstance : myClass = new myClass();
and after this you can call methods from it:
myInstance[functionName]();

Or if you have a static method from other class you should be able to do this:

myClass[myStaticFunctionName]();

Dunno if this works but you should give it a try.