RemoveEventListener:为属性查找提供的nil密钥

时间:2017-05-04 02:31:46

标签: lua corona

我创建了一个组,并添加了以下调用函数的事件侦听器:

catinBalloon:addEventListener( "touch", catinBalloontouch) 

随后,我将两个对象插入到catinBalloon组中,并将该组添加到表中:

table.insert( catandBalloonTable, #catandBalloonTable+1, catinBalloon )

作为另一个函数的一部分,我想循环遍历表以删除监听器事件。

for i = #catandBalloonTable, 1, -1 do
print_r(catandBalloonTable[i])
    catandBalloonTable[i]:removeEventListener( "touch", catinBalloontouch) 

end

错误之前print_r的输出是:

04:20:03.364  table: 0DACC088 {
04:20:03.364    [_proxy] => userdata: 0DAD20E0
04:20:03.364    [_functionListeners] => table: 0DACC088 {
04:20:03.364                              [touch] => table: 0DADA0C0 {
04:20:03.364                                           [1] => function: 04080198
04:20:03.364                                           [2] => function: 0DA0E6E0
04:20:03.364                                         }
04:20:03.364                            }
04:20:03.364    [activeObjectWord] => "ant"
04:20:03.364    [_class] => table: 0DACC088 {
04:20:03.364                  [removeEventListener] => function: 04245CE8
04:20:03.364                  [addEventListener] => function: 04247968
04:20:03.364                  [__index] => table: 0425AE40 {
04:20:03.364                                 *table: 0425AE40
04:20:03.364                               }
04:20:03.364                }
04:20:03.364    [removeSelf] => function: 0433B9B8
04:20:03.364    [active] => "yes"
04:20:03.364    [activeObjectSound] => userdata: 0A0CAAE0
04:20:03.364  }

错误是:

04:20:03.364  ERROR: nil key supplied for property lookup.
04:20:03.364  stack traceback:
04:20:03.364    [C]: ?
04:20:03.364    ?: in function 'removeEventListener'
04:20:03.364    ?: in function 'removeEventListener'
04:20:03.364    ?: in function 'removeEventListener'

谢谢。

1 个答案:

答案 0 :(得分:2)

当您在此范围内不存在您提供的处理程序函数(catinBalloontouch)时,会出现此错误。解决问题的一种方法是使用前向参考。

-- top of your file
local catinBalloontouch 
...
function catinBalloontouch( event ) -- now you don't need use key word local
    ...
end
...
for i = #catandBalloonTable, 1, -1 do
print_r(catandBalloonTable[i])
    catandBalloonTable[i]:removeEventListener( "touch", catinBalloontouch) 

end