a problem with object:removeSelf() after a collision

Hey, I made a “main.lua” file that leads to “game.lua” file,

I have circle that appears every = 800ms, and I have an Orb to throw at the circle , so that the circle should be removed when the collision (e.phase == “ended”)

the circle:removeSelf() works on the first circle, but the  circle after the first circle disappear does’t work with the function, and there’s an error attempt to call method ‘removeSelf’ (a nil value)

here’s the whole game.lua file code :

[lua]local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight

local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

function newCircle()
circle = display.newImage(“circle.png”)
circle.x = 270; circle.y = 30;
physics.addBody(circle, “static”, {radius = 43})

function removeCircle(e)
if e.phase == “began” then
print(“collision started”)
elseif e.phase == “ended” then
print(“collision ended”)
circle:removeSelf()
timer.performWithDelay(800, newCircle, 1);

end
end

circle:addEventListener(“collision”, removeCircle)

end

function spawnOrb()
orb = display.newImage(“orb.png”);
orb.x = 60; orb.y = 400
physics.addBody(orb, “dynamic”, {radius = 44})

function touchOrb(e)
if e.phase == “began” then
print(“orb began”)
display.getCurrentStage():setFocus(orb)
elseif e.phase == “ended” then
print(“orb ended”)
orb:applyLinearImpulse(((e.xStart - e.x)/2), ((e.yStart - e.y)/2), orb.x, orb.y)
display.getCurrentStage():setFocus(nil)
timer.performWithDelay(100, spawnOrb, 1);
end
end

orb:addEventListener(“touch”, touchOrb)

end

timer.performWithDelay(0, spawnOrb, 1);
timer.performWithDelay(0, newCircle, 1);

end

function scene:enterScene(e)
circle:addEventListener(“collision”, removeCircle)
end

function scene:exitScene(e)
end

function scene:destroyScene(e)
end

scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)

return scene
[/lua]

Try this one

  1. function newCircle()
  2. circle = display.newImage(“circle.png”)
  3. circle.x = 270; circle.y = 30;
  4. physics.addBody(circle, “static”, {radius = 43})
  5. –Add below line <----------------------
  6. circle:addEventListener(“collision”, removeCircle)

This will add the EventListner to the new circle. Let me know if it works.

You are using the same variable “circle” for all circles, when you create another circle the reference to the previous object is lost.

You can access each circle from the “removeCircle(e)” function via the “e” parameter which contains the two objects that collided (e.object1 and e.object2). Object1 is the target object (the object to which the collision listener was applied to).

function removeCircle(e) &nbsp; &nbsp; e.object1:removeSelf() -- this removes the circle, use this instead of circle:removeSelf() end

jscript12 miguel164777

thank you guys so much for your help, but both of the answers didn’t work, and I knew where the mistake was,

there was two lines of : 

  1. circle:addEventListener(“collision”, removeCircle);

one at line 26, and another one at line 49, after I removed line 49, the code worked very well, and I didn’t really understand why!

 

Try this one

  1. function newCircle()
  2. circle = display.newImage(“circle.png”)
  3. circle.x = 270; circle.y = 30;
  4. physics.addBody(circle, “static”, {radius = 43})
  5. –Add below line <----------------------
  6. circle:addEventListener(“collision”, removeCircle)

This will add the EventListner to the new circle. Let me know if it works.

You are using the same variable “circle” for all circles, when you create another circle the reference to the previous object is lost.

You can access each circle from the “removeCircle(e)” function via the “e” parameter which contains the two objects that collided (e.object1 and e.object2). Object1 is the target object (the object to which the collision listener was applied to).

function removeCircle(e) &nbsp; &nbsp; e.object1:removeSelf() -- this removes the circle, use this instead of circle:removeSelf() end

jscript12 miguel164777

thank you guys so much for your help, but both of the answers didn’t work, and I knew where the mistake was,

there was two lines of : 

  1. circle:addEventListener(“collision”, removeCircle);

one at line 26, and another one at line 49, after I removed line 49, the code worked very well, and I didn’t really understand why!