remove multiple objects

I am trying to remove multiple objects when they fall down . When I try to do this only 1 ball removes itself and the other two stay . How can I remove all when I touch them individually ? 

local numberBall = 3 local function spawnBall() for i = 1 , numberBall do Ball = display.newCircle(60, 60, 13) Ball.x = math.random(10, 300); Ball.y = 50; Ball:setFillColor( 0, 5, 0 ) transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); Ball:applyLinearImpulse(-.05, .05, 0, 0) Ball.touch = touch end end spawnBall()

function Ball.touch( self, event ) --line 55 if ( event.phase == "ended" ) then display.remove(self) end end function scene:show(event) Ball:addEventListener("touch") end function scene:hide(event) Ball:removeEventListener("touch") composer.removeScene( "restart" ) end

I think that’s because you declared Ball object global. Can you try this:

local Ball = display.newCircle(60, 60, 13)

When I put it local , I get this error :

:51: attempt to index global 'Ball' (a nil value)

function Ball:touch( event ) --line 51 if ( event.phase == "ended" ) then display.remove(self) end end

Remove

Ball.touch = touch

and change

Ball:addEventListener("touch")

to

Ball:addEventListener("touch", handleTouch)

and move that line below

Ball:applyLinearImpulse(-.05, .05, 0, 0)

Then change

function Ball.touch( self, event ) --line 55

to

function handleTouch(event)

It should probably work. Good luck.

Now I’m getting this error :

76: attempt to index global 'Ball' (a nil value)

local function spawnBall() for i = 1 , numberBall do local Ball = display.newCircle(60, 60, 13) Ball.x = math.random(10, 300); Ball.y = 50; Ball:setFillColor( 0, 5, 0 ) transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); end end spawnBall() timerrr =timer.performWithDelay( 3000, spawnBall, 0 ) --fire every 10 seconds local timeLimit = 6 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) local function handleTouch(event) if ( event.phase == "ended" ) then display.remove(Ball) end end

function scene:show(event) Ball:addEventListener("touch", handleTouch) end function scene:hide(event) Ball:removeEventListener("touch", handleTouch) composer.removeScene( "restart" ) end

I think that’s because you declared Ball object global. Can you try this:

local Ball = display.newCircle(60, 60, 13)

When I put it local , I get this error :

:51: attempt to index global 'Ball' (a nil value)

function Ball:touch( event ) --line 51 if ( event.phase == "ended" ) then display.remove(self) end end

Remove

Ball.touch = touch

and change

Ball:addEventListener("touch")

to

Ball:addEventListener("touch", handleTouch)

and move that line below

Ball:applyLinearImpulse(-.05, .05, 0, 0)

Then change

function Ball.touch( self, event ) --line 55

to

function handleTouch(event)

It should probably work. Good luck.

Now I’m getting this error :

76: attempt to index global 'Ball' (a nil value)

local function spawnBall() for i = 1 , numberBall do local Ball = display.newCircle(60, 60, 13) Ball.x = math.random(10, 300); Ball.y = 50; Ball:setFillColor( 0, 5, 0 ) transition.to( Ball, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Ball, "dynamic", { density=0.1, bounce=0.1, friction=0.1, radius=0 } ); end end spawnBall() timerrr =timer.performWithDelay( 3000, spawnBall, 0 ) --fire every 10 seconds local timeLimit = 6 timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 24) timeLeft:setTextColor(255,0,0) local function handleTouch(event) if ( event.phase == "ended" ) then display.remove(Ball) end end

function scene:show(event) Ball:addEventListener("touch", handleTouch) end function scene:hide(event) Ball:removeEventListener("touch", handleTouch) composer.removeScene( "restart" ) end