Hi, I’m working on an explosion, and I cant seem to get rid of said explosion. Whenever I run my code, it always says that the object is already removed, though it clearly hasn’t. I can remove the listener without much problem.
--Creates the explosion, pushing everything out of the circle (blastradius) function setExplosion(self, event) forceX = ( event.other.x - self.x ) \* 3 forceY = ( event.other.y - self.y ) \* 3 event.other:applyForce(forceX, forceY, event.other.x, event.other.y) timer.performWithDelay(10, function() circle:removeEventListener("collision", circle) circle:removeSelf() end, 1) end
--Draws the explosion radius function drawExplosion(eventX, eventY) return function() circle = display.newCircle( eventX +20, eventY +20, 150 ) circle.myName = "circle" game:insert(circle) circle:setFillColor(100,100,100, 100) physics.addBody( circle, "static", {isSensor = true} ) circle.collision = setExplosion circle:addEventListener( "collision", circle ) end end
--Execute on bomb collision function onCollision(event) hit = true timer.performWithDelay (10, drawExplosion(event.x, event.y) ) bomb:removeEventListener("touch", circleTouched) bomb:removeEventListener ( "collision", bomb ) bomb:removeSelf() end
--Check if bomb hit function checkHit(event) hit = false bomb.collision = onCollision bomb:addEventListener( "collision", bomb ) end
--Shoot the bomb function circleTouched(event) if event.phase == "began" then display.getCurrentStage():setFocus(bomb) elseif event.phase == "ended" then bomb:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, bomb.x, bomb.y) -- with 250 timeout so no collission before airborn timer.performWithDelay ( 250, checkHit ) display.getCurrentStage():setFocus(nil) end end
I just cant figure out why it gives me the error, saying that the object is already removed (the circle).
