I’m new to both Lua and Corona, and want to know the proper way to dispose of a sprite once it is offscreen. I set up an event listener on enterFrame to check if the sprite is offscreen, and in that handler cycle through my sprite table to see if such a case occurs. I know the test works, because I’ve tried simply resetting the x and y coordinates of the sprite and it bounces back to where I set those values.
However, if I try either of the first three commented out lines below, I get an error that I’m trying to compare a number with nil. If I try the fourth commented out line, it complains that I’m trying call remove on a nil value. If I just set it to nil and leave the lines commented out, I don’t get the error, but the memory usage never goes down.
BTW, the sprites are all traveling in the same direction, which is why the test only checks if the sprite is off the screen to the right or bottom. Also, the sprite is both part of a group and a table (orbs).
for i,v in pairs(orbs)
do
if v then
if v.x \> display.contentWidth + 100 or v.y \> display.contentHeight + 100
then
--group:remove(v)
--v:removeSelf()
--orbs.remove(orbs, i)
--display.remove(v)
v = nil
end
end
end
[import]uid: 58455 topic_id: 11241 reply_id: 311241[/import]