This is the code:
local bottombar = display.newLine(0, 480, 340, 480)
physics.addBody(bottombar, "static", {isSensor = true})
function bottombar:collision(event)
if(event.phase == "began") then
a = a + 1
print(a)
if(a == 4) then
timer.cancel(timer1)
storyboard.gotoScene("scene_results")
end
end
end
bottombar:addEventListener("collision", bottombar)
end
The game generate balls who fall and and i want to stop the game when 3 balls pass thru the bottom line.Variable 'a' initially is 0,when the first ball pass thru the line in console 'a' is 1,when the second ball pass thru the line the console shows me that 'a' is 2 and on the next line 3 like this:
1 => first ball
2 }
}=> second ball
3 }
If i make if(a == 10)
for 1st ball 'a' is 1,for 2nd ball 'a' is (2, 3),for the 3th 'a' is (4, 5, 6) and when the 4th ball passes thru the line 'a' is (7, 8, 9, 10).
Or if i destroy 3 balls(self:removeSelf()
when i touch them) and i let the 4th ball to pass thru line the 'a' become:
1
2
3
4
I'm sorry if i have grammatical mistakes.
This is the whole code, I can't figure out what i'm doing wrong:
local a = 0
local score = 0
local function game()
local pop = audio.loadSound("pop.WAV")
local masterVolume = audio.getVolume()
local function createCircle()
function onCircleTouch(self, event)
if(event.phase == "began") then
timer.performWithDelay(1, function()
self:removeSelf()
audio.play(pop,{channel=0,loops=0})
score=score+10
globals.score = score
end )
end
return true
end
local circle = display.newImage("ballon2.png",math.random(10,300),0)
physics.addBody( circle, "dynamic", { desity=1.0,friction=0 })
circle.touch = onCircleTouch
circle:addEventListener("touch", circle)
end
local function balls(event)
createCircle()
local bottombar=display.newLine(0, 480, 340, 480)
physics.addBody(bottombar, "static" ,{isSensor=true})
function bottombar:collision(event)
if(event.phase == "began") then
a=a+1
print (a, event.other)
if( a == 3) then
timer.cancel(timer1)
storyboard.gotoScene( "scene_results" )
end
end
end
bottombar:addEventListener("collision", bottombar)
end
timer1=timer.performWithDelay( 1000, balls, 0 )
end
game()