Two balls function

I am trying to create a function that if the user taps one ball it disappears and if the other one is also tapped , the user goes to the next level . But it’s complicated because I don’t know what to do 

-- create image of a ball Ball1 = display.newCircle(100, 100, 10) physics.addBody(Ball1, "dynamic", {friction=2}) Ball1:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball1) -- create image of a ball Ball2 = display.newCircle(100, 100, 10) physics.addBody(Ball2, "dynamic", {friction=2}) Ball2:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball2)

functions

 function onBallTap( event ) Ball1 = remove.self() Ball1 = nil end function onBallTap( event ) Ball2 = remove.self() Ball2 = nil end function scene:enterScene(event) Ball1:addEventListener( "tap", onBallTap ) Ball2:addEventListener( "tap", onBallTap ) end function scene:exitScene(event) display.remove(Ball) Ball1:removeEventListener( "tap", onBallTap ) Ball2:removeEventListener( "tap", onBallTap ) end

You can’t have 2 functions named the same (“onBallTap”). Well, you can, but the second one will overwrite the first one, so your code won’t work. That being said, I’m not suggesting you write a separate function for each ball. Instead, assign some custom property to each ball, then detect “event.target.myCustomPropertyName” in the touch listener, and decide from there what logic (going to next level, whatever) you want to do.

Brent

What custom property ?

Call the fuctions Ball1.handleTap and Ball2.handleTap

You cannot call 2 the function the same name

And of course change the event listener accordingly.

it still doesn’t work . when I click one both disappear .

I’m getting this error 

Runtime error

C:\Users\Brandon\Documents\Corona Projects\StopTheBall\storyboard.lua:1641: C:\U

sers\Brandon\Documents\Corona Projects\StopTheBall\game8.lua:89: attempt to inde

x global ‘Ball2’ (a nil value)

stack traceback:

        [C]: in function ‘error’

        C:\Users\Brandon\Documents\Corona Projects\StopTheBall\storyboard.lua:16

41: in function ‘gotoScene’

        C:\Users\Brandon\Documents\Corona Projects\StopTheBall\restart7.lua:43:

in function <C:\Users\Brandon\Documents\Corona Projects\StopTheBall\restart7.lua

:41>

        ?: in function <?:221>

-- create image of a ball Ball1 = display.newCircle(100, 100, 10) physics.addBody(Ball1, "dynamic", {friction=2}) Ball1:applyLinearImpulse(-.05, .05, 0, 0) Ball1:setStrokeColor( 0, 3, 0 ) screenGroup:insert(Ball1) -- create image of a ball Ball2 = display.newCircle(100, 100, 10) physics.addBody(Ball2, "dynamic", {friction=2}) Ball2:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball2)

 function Ball1.handleTap( event ) Ball1 = remove.self() Ball1 = nil end function Ball2.handleTap( event ) --line 89 where the problem is Ball2 = remove.self() Ball2 = nil end

I think i see your problem. You either need to set a small timer or you need to set focus. Here is both method’s.

SETFOCUS –

local Ball1 = display.newCircle( 100, 100, 10 ) Ball1:setFillColor( 1, 0, 0 ) local Ball2 = display.newCircle( 100, 100, 10 ) function removeBall1(event) if event.phase == "ended" then display.remove(Ball1) Ball1:removeEventListener( "touch", removeBall1 ) end end local function removeBall2(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "ended" then display.remove(Ball2) Ball2:removeEventListener( "touch", removeBall2 ) Ball1:addEventListener( "touch", removeBall1 ) display.getCurrentStage():setFocus(nil) end end Ball2:addEventListener( "touch", removeBall2 )

TIMER –

local Ball1 = display.newCircle( 100, 100, 10 ) Ball1:setFillColor( 1, 0, 0 ) local Ball2 = display.newCircle( 100, 100, 10 ) function removeBall1(event) if event.phase == "ended" then display.remove(Ball1) Ball1:removeEventListener( "touch", removeBall1 ) end end local function removeBall2(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "ended" then display.remove(Ball2) Ball2:removeEventListener( "touch", removeBall2 ) local function addLis() Ball1:addEventListener( "touch", removeBall1 ) end timer.performWithDelay( 10, addLis, 1 ) end end Ball2:addEventListener( "touch", removeBall2 )

I’d stick with the setFocus method though. 

Will this fix my problem ?

Well there is only one way to find out. Give it a go. 

It works (Thanks :slight_smile: ) but how do I make it go to another scene when both balls are clicked ?

Just use the gotoScene and put it into the function that removes the second ball. 

I tried but it didn’t work . and since I put in the go to scene whenever I click the first ball , it goes into the other scene . 

Referring to my code. 

function removeBall1(event) if event.phase == "ended" then display.remove(Ball1) Ball1:removeEventListener( "touch", removeBall1 ) --Put gotoScene here. end end

But I thought you said i have to put it in the function that takes out the second ball ?

That does take the second ball. I just named them incorrectly. Sorry.

If I touch the balls while it’s in the air nothing happens . I have to wait till they stay on the floor and as soon as I touch the second ball , it goes into the next scene . 

function removeBall1(event) if event.phase == "ended" then display.remove(Ball1) Ball1:removeEventListener( "touch", removeBall1 ) end end function removeBall2(event) if event.phase == "began" then display.getCurrentStage():setFocus( event.target ) elseif event.phase == "ended" then display.remove(Ball2) storyboard.gotoScene("game9", "fade", 400) Ball2:removeEventListener( "touch", removeBall2 ) Ball1:addEventListener( "touch", removeBall1 ) storyboard.gotoScene("game9", "fade", 400) display.getCurrentStage():setFocus(nil) end end function scene:enterScene(event) Ball2:addEventListener( "touch", removeBall2 ) end

If I touch the balls it has no effect . I have to wait for them to completely stop moving to touch them and get an effect 

Never mind they work 

You can’t have 2 functions named the same (“onBallTap”). Well, you can, but the second one will overwrite the first one, so your code won’t work. That being said, I’m not suggesting you write a separate function for each ball. Instead, assign some custom property to each ball, then detect “event.target.myCustomPropertyName” in the touch listener, and decide from there what logic (going to next level, whatever) you want to do.

Brent