# How can I clear the entire display in Corona?

How can I clear the entire display? I have tried doing display.remove(obj) and obj:removeSelf() on everything that was drawn, but it seems to miss a few things and I am unsure why.

A very basic version of this part would look like this:

Runtime:addEventListener("accelerometer", onShake)

--Initialize arrScreen and other variables

function onShake (event)
if event.isShake then
--for loops
display.remove(arrScreen[i][j])
--arrScreen[i][j]:removeSelf()
--end for loops
end
end

function draw(event)
if event.phase == "began" then
arrScreen[event.x][event.y]=display.newRect(event.x,event.y,size,size)
lastPoint.x=event.x
lastPoint.y=event.y
elseif event.phase ~= "ended" then
arrScreen[event.x][event.y]=display.newLine(event.x,event.y,lastPoint.x,lastPoint.y)
lastPoint.x=event.x
lastPoint.y=event.y
end
end


With Corona SDK, you normally group things such as sprites.

myGroup = display.newGroup() -- create a new group
myGroup:insert(sprite1) -- insert sprite1 in it
myGroup:insert(sprite2) -- insert sprite2 in it
myGroup:removeSelf() -- remove everything in this group
myGroup = nil -- nil the group's reference


To clear the entire screen, you have to remove all display objects you have created. I would recommend creating a table and inserting all objects into it.

objectsOnScreen = {}
-- Everytime you create a new display object, add it to the table
local newObject = display.newImage("foo.png")
table.insert(objectsOnScreen, newObject)


Then when you want to remove all objects from screen:

local function removeAllScreenObjects()
for i=1, #objectsOnScreen do
objectsOnScreen[i]:removeSelf()
-- Can also use: objectsOnScreen[i].isVisible = false
objectsOnScreen[i] = nil
end
end


Another option is to create a rectangle the size of the screen and drawing it on top of everything.

Hope this helps.