Properly Removing Objects

I'm making a game where you play as a man and you must dodge blocks falling from above. I want to make the blocks remove themselves when they reach a certain height, so I created the following code to spawn and control a block. It seems to work correctly, but I'm not sure if the blocks are being completely removed... How can I tell? Can anyone tell if I'm removing them correctly? Any other advice would also be great!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
--Spawn Box--------------------------------------------------------------------------------------
local function spawnBox()
        local box = display.newRect(mRand(0, 9) * (pixelSide*8), -pixelSide*6, pixelSide*7, pixelSide*7);
        setColor(box, mRand(0, 7));
        
        function box:enterFrame(event)
                self.y = self.y + pixelSide;
                
                if (self.y >= _H-(pixelSide*19.5)) then
                        self:removeSelf();
                        Runtime:removeEventListener("enterFrame", box);
                        box = nil;
                        self = nil;
                end
        end
        
        Runtime:addEventListener("enterFrame", box);
end
-------------------------------------------------------------------------------------------------

self = nil doesn't usually make the object nil, since the object was used to call the function.

Perhaps create a forward declaration so another code block can access the object, and nil it out if removeSelf() is called (or have the block that calls removeSelf() call an external function that nils out the object's variable.

I was wondering, does a display object automatically remove itself if the group it's in calls removeSelf()?

Also, is the memory used by a variable automatically recycled when the block of code it was created in terminates?

1) Yes
2) It depends on how you have your cleanup function

views:1753 update:2011/11/14 9:16:56
corona forums © 2003-2011