Proper way to dispose of a sprite?

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]

I use the director class and once I’ve created the sprite I insert it into my group. Upon exiting (changing the scene) director cleans it for me.

[import]uid: 29676 topic_id: 11241 reply_id: 40785[/import]

Yes, he’s removing an entire group at a time. I want to remove one object from a group containing many. Maybe it’s not possible and I have to manage my scenes to not contain too many objects.

Do you use director to go from the current game play scene to the main menu? Wouldn’t that destroy your scene and force the user to start over if they wanted to resume play?

Alas, despite the same old promises, GC languages make memory management much harder than with C++, where smart pointers and the STL do the work for you. :-/ [import]uid: 58455 topic_id: 11241 reply_id: 40808[/import]

I director-ized my code today (awesome work, Ricardo!), and confirmed my suspicion that the game scene gets re-initialized when you go to the menu and back. I may try to fiddle with that and see if I can cache the game scene and pick up where I left off. A “Resume” function on the main menu pretty much needs this behavior. [import]uid: 58455 topic_id: 11241 reply_id: 40916[/import]