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