Event error

I have a problem . When the ball falls from the screen , and the user clicks it , it’s suppose to disappear . But it doesn’t . I get an error that says :

game.lua:50: attempt to index local 'event' (a nil value)

This is the code :

function Ball:touch( self, event ) if ( event.phase == "ended" ) then --line 50 print( "Touch event began . " ) end end

local numberBall = 3 --local variable; amount can be changed local function clearBall( thisBall ) display.remove( Ball ) Ball = nil end local function spawnBall() for i=1, numberBall do Ball = display.newCircle(60, 60, 13) -- when you create it with 'local', it won't be the same Ball object but a new one Ball:setFillColor( 0, 5, 0 ) physics.addBody(Ball, "dynamic", {density=0.1, bounce=0.1, friction=0.1, radius=0}) Ball:applyLinearImpulse(-.05, .05, 0, 0) -- you first need to add a physics body before you apply linear impulse Ball.id = "Ball" Ball.x = math.random(10, 300); Ball.y = 50; 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 } ); -- redundant because you have already added a physics body to Ball end end

Also when I click on other balls they don’t do anything I have to click a specific ball to get a reaction . How do I change this ?

function Ball:touch( self, event )

should be

function Ball:touch( event )

or

function Ball.touch( self, event )

It works but only one ball disappears 

I removed the transition, since I don’t combine transitions with physics (generally).

So modify this as you need…

local function touch(self,event) display.remove( self ) end 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 ) physics.addBody(ball, "dynamic", {density=0.1, bounce=0.1, friction=0.1, radius=0}) ball:applyLinearImpulse(-.05, .05, 0, 0) ball.touch = touch ball:addEventListener("touch") end end spawnball()

Now i’m getting this error 

game.lua:55: attempt to index global 'Ball' (a nil value) function Ball.touch( self, event ) --line 55 if ( event.phase == "ended" ) then display.remove(Ball) end end
function Ball.touch( self, event ) --line 55 if ( event.phase == "ended" ) then display.remove(self) end end

Still getting the same error 

function Ball:touch( self, event )

should be

function Ball:touch( event )

or

function Ball.touch( self, event )

It works but only one ball disappears 

I removed the transition, since I don’t combine transitions with physics (generally).

So modify this as you need…

local function touch(self,event) display.remove( self ) end 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 ) physics.addBody(ball, "dynamic", {density=0.1, bounce=0.1, friction=0.1, radius=0}) ball:applyLinearImpulse(-.05, .05, 0, 0) ball.touch = touch ball:addEventListener("touch") end end spawnball()

Now i’m getting this error 

game.lua:55: attempt to index global 'Ball' (a nil value) function Ball.touch( self, event ) --line 55 if ( event.phase == "ended" ) then display.remove(Ball) end end
function Ball.touch( self, event ) --line 55 if ( event.phase == "ended" ) then display.remove(self) end end

Still getting the same error