简体   繁体   English

尝试调用方法“ removeSelf”(nil值)

[英]attempt to call method 'removeSelf' (a nil value)

For some reason I can't remove a group object, even though I check if it's nil or not 由于某种原因,即使我检查它是否为nil,也无法删除其组对象

First thing I tried: 我尝试过的第一件事:

if playGroup~=nil then 
   playGroup:removeSelf() 
end

ERROR: Attempt to remove an object that's already been removed from the stage or whose parent/ancestor group has already been removed. 错误:尝试删除已经从舞台中删除的对象或其父级/祖先组已删除的对象。

I also tried this: 我也试过这个:

for k,v in pairs(playGroup) do
    if k ~= nil then
        k:removeSelf()
    end
end

ERROR: attempt to call method 'removeSelf' (a nil value) 错误:尝试调用方法“ removeSelf”(nil值)

You should destroy v and not k: 您应该销毁v而不是k:

for k,v in pairs(playGroup) do
  if v ~= nil then
     v:removeSelf()
  end
end

You do not need to remove objects from a group, Corona does it for you when you remove the group. 您无需从组中删除对象,Corona会在您删除组时为您完成。 So the loop (even after the fix proposed by hades2510), is not required. 因此,不需要循环(即使在hades2510建议的修复之后)。

The error you get, "Attempt to remove an object that's already been removed..." suggests that playGroup is a regular table rather than a display object. 您收到的错误“尝试删除已经被删除的对象...”表明playGroup是常规表而不是显示对象。 Calling removeSelf on a display object removes Corona-related entries, leaving the object as a regular table. 在显示对象上调用removeSelf会删除与电晕相关的条目,从而将该对象保留为常规表。 So there are at least these possibilities: 因此,至少存在以下可能性:

  • it could be that your code has already removed playGroup as part of another event handler, or 可能是您的代码已经删除playGroup作为另一个事件处理程序的一部分,或者
  • the handler was called a second time, or 处理程序被第二次召集,或者
  • playGroup was created as a regular table rather than a display object. playGroup被创建为常规表而不是显示对象。

One thing you could do is set playGroup = nil after removeSelf so that if Corona comes across that if block again, it will not attempt to remove it again: 有一两件事你可以做的是设置playGroup = nilremoveSelf因此,如果电晕遇到的是if再次阻止,也不会尝试再次将其删除:

if playGroup ~= nil then 
    playGroup:removeSelf() 
    -- now playGroup is plain table
    -- dont' want to run this block of code again: 
    playGroup = nil
end

If this does fix the problem, it is probably still worth determining why the if block gets called after playGroup has been removed from display. 如果这样做确实可以解决问题,那么仍然有必要确定为什么在从显示组中删除playGroup之后调用if块的原因。 A print statement, without setting playGroup to nil, will do the trick: 在不将playGroup设置为nil的情况下playGroup print语句即可playGroup目的:

if playGroup ~= nil then 
    print('WARNING: Going to remove play group from display')
    playGroup:removeSelf() 
    print('WARNING: Play group removed from display')
end

Once you know the reason, you can add back the playGroup = nil . 一旦知道了原因,就可以添加回playGroup = nil

Make sure you have inserted the object in your table or group. 确保已将对象插入表或组中。 If you don't insert the object in table or group and try to remove it, then this error will produce. 如果不将对象插入表或组中并尝试将其删除,则将产生此错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM