Question

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()
Was it helpful?

Solution 2

Finally i found the solution for my problem. I put beneath local score = 0 this:

local bottombar=display.newLine(0, 480, 340, 480)
physics.addBody(bottombar, "static" ,{isSensor=true})

and beneath this i put bottombar:addEventListener("collision", bottombar) .

OTHER TIPS

I am not able to reproduce the problem: I took the code you posted, created 3 buttons that I added to physics as dynamic objects, and let them fall towards the line. I get one printout per body. Something is happening elsewhere in your code. Can you create a minimal standalone example, it may become apparent what the issue is.

Some things to check:

  1. verify that you do not remove the colliding ball during a collision event. Must delay the removal
  2. change print to print(a, event.other) to see if the additional collisions involve different balls, which would indicate that the physics engine is still evolving the balls that have passed the line

A note on style: Lua does not require () around if condition, () is redundant and just adds noise.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top