I am working on a flash game that requires me to instantiate objects from a class into the scene at runtime and and delete these objects through code.
This works fine and i can dynamically add and remove instances from the game, however after about two minutes in it starts lagging extremely and will eventually crash alltogether.
I used numchildren() to trace how many objects are in the scene and it appears that some of the instances aren’t being deleted properly because the number traced will increase and eventually reach near 2000 objects…
I am using removeChild() to delete the objects and am trying to figure out why these objects are still in the scene.
I read somewhere that objects may still use cpu if they haven’t properly been reset.
I am still new to working in flash and my main question is – how do I find out which objects aren’t being deleted and how do I remove them entirely from the game?
What else do I need to add to the code other than the simple removeChild() part?
Any help would be greatly appreciated.
Please provide the code part where removeChild() method is used.
Generally you should use removeChild() this way:
additionally note that removing objects form display list, won’t exclude them from memory, if any references are left on those objects. In my example reference to one MovieClip object remains after all Movieclips are removed from stage, so it will occupy some portion of VM’s memory and Garbage Collector won’t clean that portion. In my example, to remove reference, I should add this line of code after while loop in onTimerComplete method: