Ball touch error

I am trying to make all the balls disappear when I touch on them but I am getting this error when I touch them :

77: attempt to index global 'Ball'

This is all the relevant code :

local numberBall = 3 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()

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) --where the error is composer.removeScene( "restart" ) end

Ball variable exists only inside spawnBall function. Use table to store all balls. I only rewrote handleTouch.

local balls = {}  ... local function handleTouch(event)      if ( event.phase == "ended" ) then           for i=#balls, 1, -1 do               local ball = table.remove(balls, i)                              if ball ~= nil then                   display.remove(ball)                   ball = nil               end          end      end end

Now I’m getting this error :

84: in function attempt to call method 'removeEventListener' 

Balls:removeEventListener("touch", handleTouch)

What does Balls is? If you use Balls to store all balls you need proper way to remove listener. For example

 function scene:hide(event)     if Balls and #Balls \> 0 then         for i=1, #Balls do             Balls[i]:removeEventListener("touch", handleTouch)         end     end     composer.removeScene( "restart" ) end

You don’t need to remove touch listeners when exiting the scene. As long as the objects are added to scene.view, they and their associated listeners will be removed when you leave the scene.

Why are you creating new topics for the same question? Is there a reason for your spamming?

https://forums.coronalabs.com/topic/66867-remove-multiple-objects/

Also, why don’t you correct the simplest typo and post here constantly?

Also, why don’t you correct the simplest typo and post here constantly?

what do you mean ?

You are just spamming the forums without trying anything by yourself. You are like “I’m getting this error, that error. Go ahead, correct it”. You are not even trying!

didn’t help

Are you watching my laptop ? How do you know i’m not trying ? I tried a whole bunch of things looked this up multiple times and didn’t find anything so I came here 

Ball variable exists only inside spawnBall function. Use table to store all balls. I only rewrote handleTouch.

local balls = {}  ... local function handleTouch(event)      if ( event.phase == "ended" ) then           for i=#balls, 1, -1 do               local ball = table.remove(balls, i)                              if ball ~= nil then                   display.remove(ball)                   ball = nil               end          end      end end

Now I’m getting this error :

84: in function attempt to call method 'removeEventListener' 

Balls:removeEventListener("touch", handleTouch)

What does Balls is? If you use Balls to store all balls you need proper way to remove listener. For example

 function scene:hide(event)     if Balls and #Balls \> 0 then         for i=1, #Balls do             Balls[i]:removeEventListener("touch", handleTouch)         end     end     composer.removeScene( "restart" ) end

You don’t need to remove touch listeners when exiting the scene. As long as the objects are added to scene.view, they and their associated listeners will be removed when you leave the scene.

Why are you creating new topics for the same question? Is there a reason for your spamming?

https://forums.coronalabs.com/topic/66867-remove-multiple-objects/

Also, why don’t you correct the simplest typo and post here constantly?

Also, why don’t you correct the simplest typo and post here constantly?

what do you mean ?

You are just spamming the forums without trying anything by yourself. You are like “I’m getting this error, that error. Go ahead, correct it”. You are not even trying!

didn’t help

Are you watching my laptop ? How do you know i’m not trying ? I tried a whole bunch of things looked this up multiple times and didn’t find anything so I came here