I do not have a problem removing the display object within a collision handler. I am having trouble removing the physics body. Here is a typical handler…
[lua] function shape:collision(e)
print(tostring(system.getTimer()) … ‘\tshape collision event: ‘… tostring(e.phase))
if(e.phase == “began”) then
if(self.isVisible) then
self.isVisible = false
end
end
if(e.phase == “ended”) then
print(tostring(system.getTimer()) … ‘\t’…self.type …’ [’…tostring(self)…’] collided with ’ … e.other.type… ’ element: ’ …tostring(e.otherElement))
function cleanUp()
print(tostring(system.getTimer()) … ‘\tcleaning up ‘…self.type …’ [’…tostring(self)…’] collision with ’ … e.other.type… ’ element: ’ …tostring(e.otherElement))
physics:removeBody(self)
self:removeSelf()
end
timer.performWithDelay(10, cleanUp,1 )
end
end[/lua]
When I do the physics.removeBody and removeSelf without the timer delay, all appears fine on the application, but Corona terminal has the same error I posted earlier. If I use the timer approach, I get this error:
bad argument #-2 to ‘removeBody’ (Proxy expected, got nil)
This is driving me crazy. I must be missing something. Any ideas?
Here is a full main.lua file that demonstrates the problem:
[lua]display.setStatusBar( display.HiddenStatusBar)
_H = display.contentHeight
_W = display.contentWidth
local physics = require “physics”
function dropShape()
local radius = 18
local shape = display.newCircle(_W/2, 46, radius )
shape.type = “shape”
function shape:collision(e)
print(tostring(system.getTimer()) … ‘\tshape collision event: ‘… tostring(e.phase))
if(e.phase == “began”) then
if(self.isVisible) then
self.isVisible = false
end
end
if(e.phase == “ended”) then
print(tostring(system.getTimer()) … ‘\t’…self.type …’ [’…tostring(self)…’] collided with ’ … e.other.type… ’ element: ’ …tostring(e.otherElement))
function cleanUp()
print(tostring(system.getTimer()) … ‘\tcleaning up ‘…self.type …’ [’…tostring(self)…’] collision with ’ … e.other.type… ’ element: ’ …tostring(e.otherElement))
physics:removeBody(self)
self:removeSelf()
end
timer.performWithDelay(10, cleanUp,1 )
end
end
shape:addEventListener(“collision”, shape);
physics.addBody(shape,{radius = radius})
end
function setUpCatcher()
catcher = display.newRect(0, 0, 64, 20)
catcher.type = “catcher”
catcher:setFillColor(255, 0, 0)
catcher:setReferencePoint(display.BottomCenterReferencePoint);
catcher.x = _W/2
catcher.y = _H-20
function catcher:collision(e)
print(tostring(system.getTimer()) …’\tcatcher collision phase: '… tostring(e.phase))
end
catcher:addEventListener(“collision”, catcher);
local density = 0.3
local friction = 0
local bounce = 0.5
local catcherShape = { }
catcherShape[“64x48”] = {
{ – Left
density=density, friction=friction, bounce=bounce,
shape={
-31,-22,
-27,-22,
-17,18,
-20,21
}
},
{ – Bottom
density=density, friction=friction, bounce=bounce,
shape={
-17,18,
15,18,
18,21,
-20,21
}
},
{-- Right
density=density, friction=friction, bounce=bounce,
shape={
27,-22,
31,-22,
18,21,
15,18
}
}
}
physics.addBody(catcher, “kinematic”, unpack(catcherShape[“64x48”]));
end
function main()
physics.start(true)
physics.setDrawMode(“hybrid”);
setUpCatcher()
dropShape()
end
main()[/lua] [import]uid: 101075 topic_id: 18290 reply_id: 76654[/import]