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]