Ball Touch

I am creating an app that requires the user to touch the ball and make it disappear but when I touch the ball I get this error :

game.lua:81: attempt to call method 'addEventListener' (a nil value)

I don’t know what’s causing the error , as I have tried to fix it for a while . This is my code :

local Balls = {} local numberBall = 3 local function spawnBall() for i = 1 , numberBall do local Balls = display.newCircle(60, 60, 13) Balls.x = math.random(10, 300); Balls.y = 50; Balls:setFillColor( 0, 5, 0 ) transition.to( Balls, { time=math.random(4000,8000), x=math.random(-10,100) , y=600, onComplete=clearBall } ); physics.addBody( Balls, "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 for i=#Balls, 1, -1 do local Balls = table.remove(Balls, i) if Balls ~= nil then display.remove(Balls) Balls = nil end end end end function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit == 0)then timer.cancel(timerr) display.remove(timeLeft) display.remove(Balls) timer.cancel(timerrr) composer.gotoScene("restart", "fade", 300) return true end end timerr = timer.performWithDelay(1000,timerDown,timeLimit) function scene:show(event) Balls:addEventListener("touch", handleTouch) end 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 canceled timerrr twice in the timerDown() function.

Also try this instead for eventListener:

Runtime:addEventListener("touch", handleTouch)

Nothing happens when I touch the balls

  1. On top you create variable Balls as table and below you create variable with same name but as display object. This is wrong. 

Try use different name.

  1. Add touch listener to display object  in spawnBall function

3.I’m not sure you need use removeEventListener in hide function at all.

This is my code 

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

local function handleTouch(event) if ( event.phase == "ended" ) then for i=#Balls, 1, -1 do local Ballss = table.remove(Ballss, i) if Ballss ~= nil then display.remove(Ballss) Ballss = nil end end end end function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit == 0)then timer.cancel(timerr) display.remove(timeLeft) display.remove(Ballss) timer.cancel(timerrr) composer.gotoScene("restart", "fade", 300) return true end end timerr = timer.performWithDelay(1000,timerDown,timeLimit) function scene:show(event) end function scene:hide(event) if Balls and #Balls \> 0 then for i=1, #Balls do end end composer.removeScene( "restart" ) end

And it doesn’t work 

It doesn’t work because I did not give you solution but some clues what is wrong. 

You canceled timerrr twice in the timerDown() function.

Also try this instead for eventListener:

Runtime:addEventListener("touch", handleTouch)

Nothing happens when I touch the balls

  1. On top you create variable Balls as table and below you create variable with same name but as display object. This is wrong. 

Try use different name.

  1. Add touch listener to display object  in spawnBall function

3.I’m not sure you need use removeEventListener in hide function at all.

This is my code 

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

local function handleTouch(event) if ( event.phase == "ended" ) then for i=#Balls, 1, -1 do local Ballss = table.remove(Ballss, i) if Ballss ~= nil then display.remove(Ballss) Ballss = nil end end end end function timerDown() timeLimit = timeLimit-1 timeLeft.text = timeLimit if(timeLimit == 0)then timer.cancel(timerr) display.remove(timeLeft) display.remove(Ballss) timer.cancel(timerrr) composer.gotoScene("restart", "fade", 300) return true end end timerr = timer.performWithDelay(1000,timerDown,timeLimit) function scene:show(event) end function scene:hide(event) if Balls and #Balls \> 0 then for i=1, #Balls do end end composer.removeScene( "restart" ) end

And it doesn’t work 

It doesn’t work because I did not give you solution but some clues what is wrong.