2015-04-03 57 views
0

我有一个函数,动作脚本3:调用字符串变量函数

function tempFeedBack():void 
    { 
     trace("called"); 
    } 

和事件侦听器完美的作品时,我直接写函数名这样的,

thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack); 

但是,当我给功能名称像这样的字符串,

thumbClip.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]()); 

它不工作!它说,TypeError: Error #1006: value is not a function.

任何想法?

回答

0

因为根本this["tempFeedBack"]()不是Function对象,这是addEventListener()功能的listener参数你明白我的错误,此外,这没有什么,因为你的tempFeedBack()函数无法返回任何值。

为了更明白,你已经写了等同于:

thumbClip.addEventListener(MouseEvent.CLICK, tempFeedBack()); 

在这里,您可以看到您已通过您的tempFeedBack()功能的listener参数的返回值,这应该是一个Function对象,但您的tempFeedBack()不能返回任何内容!

所以,只是为了更多地解释,如果你想要的是什么,你已经写了工作,你应该做这样的事情:

function tempFeedBack():Function 
{ 
    return function():void { 
      trace("called"); 
     } 
} 

但是,我认为你的意思是:

// dont forget to set a MouseEvent param here 
function tempFeedBack(e:MouseEvent):void 
{ 
    trace("called"); 
} 
stage.addEventListener(MouseEvent.CLICK, this["tempFeedBack"]); 
// you can also write it 
stage.addEventListener(MouseEvent.CLICK, this.tempFeedBack); 

希望能有所帮助。

+0

啊,太棒了!有效! – nikhilk 2015-04-03 15:19:50