For some reason I can't remove a group object, even though I check if it's nil or not
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)
You should destroy v and not 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. So the loop (even after the fix proposed by hades2510), is not required.
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. Calling removeSelf
on a display object removes Corona-related entries, leaving the object as a regular table. So there are at least these possibilities:
playGroup
as part of another event handler, or 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:
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. A print statement, without setting playGroup
to nil, will do the trick:
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
.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.