Removing Objects Properly

I was wondering if someone can help explain the code block below. I found it in the App Programming Guide (http://developer.anscamobile.com/content/app-programming-guide).

I understand assigning an event listener to the [lua]rect[/lua] object and then assigning a variable to the “tapped” [lua]rect[/lua] within the function. But I’m a little confused about assigning variables to the parent and how this is allowing the object to be garbage collected properly.

[lua]local rect =display.newRect(0,0,10,10)
rect:setFillColor(255,255,255)

local function removeOnTap(event)
local t = event.target
local parent = t.parent
parent:remove( t )
return true
end

rect:addEventListener(“tap”, removeOnTap )[/lua] [import]uid: 29663 topic_id: 5739 reply_id: 305739[/import]

The parent of a display object is the display group it is part of. By default that display group is the stage, but you could insert them into other display groups.

That code is more complicated than it needs to be though. This does the same thing:

local function removeOnTap(event) event.target:removeSelf() return true end [import]uid: 12108 topic_id: 5739 reply_id: 19812[/import]