# Calling a function name passed as a parameter?

**URL:** <https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858>\
**Category:** flash\
**Created:** [June 6, 2006, 7:13pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858 "2006-06-06T19:13:15Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![tetraconz](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@tetraconz](https://forum.kirupa.com/u/tetraconz)\
**Post date:** [June 6, 2006, 7:13pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858/1 "2006-06-06T19:13:15Z")

</div>

I have created a function that applies a set of tweens to a movieclip. This function has a number of parameters, one of which I need to be the name of a function that is called when the tween is finished. The tweening itself works just fine. It is getting it to call a function specified as a string or other means passed as a parameter that is not working.

Here is an abbreviated example of the code:

[FONT=Courier New]//Inside the movieclip [/FONT]  
[FONT=Courier New]doSpecialTween(this, 100, “callWhenDone()”);[/FONT]

[FONT=Courier New]//In root[/FONT]  
[FONT=Courier New]\_global.doSpecialTween = function(objectName, amount, endingFunction) {[/FONT]  
[FONT=Courier New]//The tween itself is working just fine[/FONT]  
[FONT=Courier New]myTween = new mx.transitions.Tween(objectName, “\_alpha”, mx.transitions.easing.None, 0, amount, 1, true);[/FONT]

[FONT=Courier New]//It is getting the function name passed as a parameter to execute[/FONT]  
[FONT=Courier New]myTween.onMotionFinished = function() { eval(endingFunction) }[/FONT]

[FONT=Courier New]}[/FONT]

[FONT=Courier New]function callWhenDone() {[/FONT]  
[FONT=Courier New]trace(“tween finished!”);[/FONT]  
[FONT=Courier New]}[/FONT]  
[FONT=Courier New][/FONT]  
Any ideas???

---

<div class="post-metadata">

**Author:** ![McGuffin](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/mcguffin/32/408_2.png) [@McGuffin](https://forum.kirupa.com/u/McGuffin)\
**Post date:** [June 6, 2006, 7:39pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858/2 "2006-06-06T19:39:58Z")

</div>

```auto

// old line:
doSpecialTween(this, 100, "callWhenDone()");
// new line (might need the quotes, not sure):
doSpecialTween(this, 100, callWhenDone);
//
//
//old line:
_global.doSpecialTween = function(objectName, amount, endingFunction) {
// new line:
_global.doSpecialTween = function (objectName:MovieClip, amount:Number, endingFunction:Function) {

```

Should get you on the right path, at least.

---

<div class="post-metadata">

**Author:** ![tetraconz](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@tetraconz](https://forum.kirupa.com/u/tetraconz)\
**Post date:** [June 6, 2006, 9:35pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858/3 "2006-06-06T21:35:07Z")

</div>

Hmm, I added the data types but still am not able to get it to work. Any other thoughts?

I am also trying \_rootendingFunction with no luck.

---

<div class="post-metadata">

**Author:** ![creatify](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@creatify](https://forum.kirupa.com/u/creatify)\
**Post date:** [June 6, 2006, 10:15pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858/4 "2006-06-06T22:15:19Z")

</div>

when using callback functions in this manner, I sometimes pass another arg - the object where that function lives to avoid scope issues. So, syntax would be like so:

[AS]

this.myFunc = function(\_func:String, \_loc:Object, \_arg:String) {  
\_loc\_func;  
}

this.callBack = function(\_argsent:String) {  
trace(\_argsent);  
}

myFunc(“callBack”, this, “this worked”);

[/AS]

---

<div class="post-metadata">

**Author:** ![TheCanadian](https://avatars.discourse-cdn.com/v4/letter/t/67e7ee/32.png) [@TheCanadian](https://forum.kirupa.com/u/TheCanadian)\
**Post date:** [June 6, 2006, 10:53pm UTC](https://forum.kirupa.com/t/calling-a-function-name-passed-as-a-parameter/189858/5 "2006-06-06T22:53:32Z")

</div>

I’d do it like this:

```auto
//Inside the movieclip 
doSpecialTween(this, 100).onMotionFinished = function() {
	trace("tween finished!");
};
//In root
_global.doSpecialTween = function(objectName, amount) {
	return new mx.transitions.Tween(objectName, "_alpha", mx.transitions.easing.None, 0, amount, 1, true);
};

```
