Ball Touch problem still happening

I posted two previous questions asking for help with my Ball touch error and I got answers but none of them helped me . So can someone please help me . When I touch the ball nothing happens but I don’t get any errors or anything the balls just don’t disappear . Can someone please help me make the balls disappear when the user touches them ? 

local Balls = {} local numberBall = 3 local function spawnBall() for i = 1 , numberBall do 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() timerrr =timer.performWithDelay( 3000, spawnBall, 0 ) --fire every 10 seconds local function handleTouch(event) if ( event.phase == "ended" ) then for i=#Balls, 1, -1 do local Ballss = table.remove(Ballss, i) Runtime:addEventListener("touch", Balls) 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

Hi there,

  1.  you have a couple of lines 

    Runtime:addEventListener(“touch”, Balls)

but your listener you name 'handleTouch. These should become Runtime:addEventListener(“touch”, handleTouch)

  1. In your handleTouch listener, you have the following:

    for i=#Balls, 1, -1 do

However, you don’t set #Balls. If you want the number of entries in the table, you can use** table.maxn(Balls)**

BUT…

it looks like you don’t ever add anything to the Balls table. You remove from it, but never insert to it.

So, it looks like you have a bit of a mishmash. It might read easier to rename some of the variables as trying to follow the difference between Balls and Ballss can be tricky :slight_smile:

PS: timeLimit isn’t set either

Just as food for thought, I rewrote it slightly to produce what you are looking for, you may be able to take the concept and embed it in your main code… You can run this independently to see it run.

This generates 3 balls at a random X position every 3 seconds.

When you tap the ball it is removed.

When your time is up (I set it to 10 seconds), it will clean up the balls and listeners and display a message.

require("physics") physics.start( ) local numberBall = 3 local timeLimit = 10 -- Time in seconds. math.randomseed( os.time() ) local grpBall = display.newGroup( ) -- listener for touching the ball. local function lstBallTouch(event) -- Using 'began' as otherwise it's when your finger is lifted which isn't as expected.      if ( event.phase == "began" ) then    grpBall:remove( event.target  )    display.remove( event.target ) -- Remove what was touched.    end end -- Spawn numberBall at random locations. (Loop X times) local function spawnBall( randomPosition ) for i= 1,numberBall do local randomPosition = math.random(10,300)     Ball = display.newCircle(60, 60, 13)     Ball.x = randomPosition     Ball.y = 50     Ball:setFillColor( 0, 5, 0 )     grpBall:insert( Ball )     Ball:addEventListener("touch", lstBallTouch)  -- Give it a listener to process a touch. 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 -- Obtain a random number for the spawned object's x position -- Wrap "spawnBall" and "randomPosition" inside a closure local fncDoSpawn = function() return spawnBall( randomPosition ) end local tmrDoSpawn = timer.performWithDelay( 3000, fncDoSpawn, 0 ) function fncCountdownTimer()     timeLimit = timeLimit-1 print (timeLimit)     if(timeLimit == 0)then print( "Times Up" )       display.remove(grpBall) -- This should remove the listeners too. local GameOver = display.newText( "Time Limit Reached", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) timer.cancel( tmrDoSpawn ) -- Stop the balls from spawning.         return true      end end -- This line will start the countdown timer. When we hit 0 everything stops. local timeLimit = timer.performWithDelay(1000,fncCountdownTimer,timeLimit)

Thanks it works 

Hi there,

  1.  you have a couple of lines 

    Runtime:addEventListener(“touch”, Balls)

but your listener you name 'handleTouch. These should become Runtime:addEventListener(“touch”, handleTouch)

  1. In your handleTouch listener, you have the following:

    for i=#Balls, 1, -1 do

However, you don’t set #Balls. If you want the number of entries in the table, you can use** table.maxn(Balls)**

BUT…

it looks like you don’t ever add anything to the Balls table. You remove from it, but never insert to it.

So, it looks like you have a bit of a mishmash. It might read easier to rename some of the variables as trying to follow the difference between Balls and Ballss can be tricky :slight_smile:

PS: timeLimit isn’t set either

Just as food for thought, I rewrote it slightly to produce what you are looking for, you may be able to take the concept and embed it in your main code… You can run this independently to see it run.

This generates 3 balls at a random X position every 3 seconds.

When you tap the ball it is removed.

When your time is up (I set it to 10 seconds), it will clean up the balls and listeners and display a message.

require("physics") physics.start( ) local numberBall = 3 local timeLimit = 10 -- Time in seconds. math.randomseed( os.time() ) local grpBall = display.newGroup( ) -- listener for touching the ball. local function lstBallTouch(event) -- Using 'began' as otherwise it's when your finger is lifted which isn't as expected.      if ( event.phase == "began" ) then    grpBall:remove( event.target  )    display.remove( event.target ) -- Remove what was touched.    end end -- Spawn numberBall at random locations. (Loop X times) local function spawnBall( randomPosition ) for i= 1,numberBall do local randomPosition = math.random(10,300)     Ball = display.newCircle(60, 60, 13)     Ball.x = randomPosition     Ball.y = 50     Ball:setFillColor( 0, 5, 0 )     grpBall:insert( Ball )     Ball:addEventListener("touch", lstBallTouch)  -- Give it a listener to process a touch. 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 -- Obtain a random number for the spawned object's x position -- Wrap "spawnBall" and "randomPosition" inside a closure local fncDoSpawn = function() return spawnBall( randomPosition ) end local tmrDoSpawn = timer.performWithDelay( 3000, fncDoSpawn, 0 ) function fncCountdownTimer()     timeLimit = timeLimit-1 print (timeLimit)     if(timeLimit == 0)then print( "Times Up" )       display.remove(grpBall) -- This should remove the listeners too. local GameOver = display.newText( "Time Limit Reached", display.contentCenterX, display.contentCenterY, native.systemFont, 48 ) timer.cancel( tmrDoSpawn ) -- Stop the balls from spawning.         return true      end end -- This line will start the countdown timer. When we hit 0 everything stops. local timeLimit = timer.performWithDelay(1000,fncCountdownTimer,timeLimit)

Thanks it works