【问题标题】:Any way to Call A Function As A String?有什么方法可以将函数调用为字符串?
【发布时间】:2017-01-30 12:21:01
【问题描述】:

我刚刚开始了一个项目,想知道您是否可以通过字符串调用函数(在事件侦听器中)。

import flash.utils.Timer;
import flash.events.Event;
import flash.events.TimerEvent;

var threesec:Timer=new Timer(3000, 1);
var whaton:String="tsecc"
threesec.start();
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);
function tsecc(tsecc:TimerEvent):void{
    trace("Hello")
    threesec.reset();
    threesec.start();
}

由于这一行,这不起作用:

threesec.addEventListener(TimerEvent.TIMER_COMPLETE, whaton);

还有这个错误代码:

1067: Implicit coercion of a value of type String to an unrelated type Function.

我知道我在做什么是非常错误的,但是有没有正确的方法来调用字符串格式的函数?

是否必须为变量添加属性,是否必须创建另一种类型的变量?

【问题讨论】:

  • 是否有正确的方法以字符串格式调用函数?或者用变量调用函数
  • 我认为@WillTower 所要求的不是您遇到的直接问题,而是您为什么要尝试以这种复杂的方式调用该函数。只需使用真实的函数名称即可。在您发布的代码中按照您想要的方式进行操作是没有意义的。

标签: actionscript-3 flash actionscript flash-cs6


【解决方案1】:
threesec.addEventListener(TimerEvent.TIMER_COMPLETE, this[whaton]);  

我使用“括号语法”来做到这一点。您可以通过在 Internet 上搜索了解更多信息。

【讨论】:

  • 我知道有这样的东西,但我不知道它是什么。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多