Spent way too long on this-
Can anyone tell me why the following blows up?
The collision call-back removes the object successfully but if you try to remove the second object sometime later, it crashes the simulator. (see removeMine() and cleanUp() below)
Thanks!
require "sprite"
display.setStatusBar( display.HiddenStatusBar )
local physics = require "physics"
physics.start()
physics.setDrawMode( "hybrid" )
local guy = display.newImage("armyguySmall.png")
physics.addBody( guy, {radius=10.0, filter={ categoryBits = 2, maskBits = 5 } } )
guy.x = 100; guy.y = 100
guy.name = "bob"
guy.pType = "enemy"
local mine1 = display.newImage("claymore3.png")
mine1.x = 100; mine1.y = 200
physics.addBody( mine1, "static", { isSensor = true, density = 1.0, friction = 1.0, bounce = 0.0, radius=10.0, filter={ categoryBits = 4, maskBits = 2 } } )
mine1.name = "mine1"
mine1.pType = "mine"
local mine2 = display.newImage("claymore3.png")
mine2.x = 30; mine2.y = 250
physics.addBody( mine2, "static", { isSensor = true, density = 1.0, friction = 1.0, bounce = 0.0, radius=10.0, filter={ categoryBits = 4, maskBits = 2 } } )
mine2.name = "mine2"
mine2.pType = "mine"
-- THIS WORKS GREAT
local function removeMine(obj)
print("removing mine 1")
mine1:removeSelf()
end
-- COLLISIONS ---------------------------------------------------------------------------------
local function onCollision( event )
local obj1, obj2 = event.object1, event.object2
if ( event.phase == "began" ) then
print( "began: " .. obj1.name .. " & " .. obj2.name )
if (obj2.pType == "enemy") then
if (obj1.pType == "mine") then
obj2:removeSelf()
if (obj1.pType == "mine") then
timer.performWithDelay(1000, removeMine(obj1), 1)
end
end
end
end
end
Runtime:addEventListener("collision", onCollision)
-- THIS BLOWS UP THE SIMULATOR
function cleanUp()
print("remove mine 2 and explode!")
mine2:removeSelf()
end
timer.performWithDelay(3000, cleanUp, 1)
[import]uid: 7587 topic_id: 2198 reply_id: 302198[/import]